Prevent Chart Data Labels from Being Obscured - c#

I am building an application using .net to automatically generate charts and save them into memory streams from data. I met the problem of data labels being obscured by other series. I enabled the following.
series.SmartLabelStyle.Enabled = true
series.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes
series.SmartLabelStyle.IsMarkerOverlappingAllowed = false
series.SmartLabelStyle.MovingDirection = LabelAlignmentStyles.Right
And data label can still be obscured by other data series, as the example shown below.
So the question is, how can I make all data labels visible and clear?
Thanks in advance.
EDIT: If that matters, I selectively enable data label on the series when a DataPoint is added.
Edit 2: I add data points one by one using Points.AddXY(SomeDate.ToOADate(), SomeValue). I go through the data, and add one point to each series, and enable the data label under some conditions. The critical part is like the following.
var index = series1.Points.AddXY(date.ToOADate(), value)
series2.Points.AddXY(date.ToOADate(), value)
series3.Points.AddXY(date.ToOADate(), value)
if (some condition)
{
series1.Points.[index].IsValueShownAsLabel = true
series2.Points.[index].IsValueShownAsLabel = true
series3.Points.[index].IsValueShownAsLabel = true
}
I set the SmartLabelStyle for each series, and then add the series to the chart, and then add data points to the series.

Related

Show image in datagridview boolean column instead of the 0/1 value

I have a WinForms desktop app with data grid view.
It loads data from Sql server and I have a bit column USRstatus in the table.
The table columns is as follows:
USRstatus(bit) , USERNAME, USERTYPE
When I load the table in my DataGridView, I want to show images instead of 0/1 values.
The images are in the resources:
Pharm.propreties.recourses.on
Pharm.propreties.recourses.off
EDIT
At the load of dgv, I created an image Column and inserted it in
DataGridViewImageColumn IMG =new
DataGridViewImageColumn();
IMG.name="status";
DGVUsers.Columns.Insert(1,IMG);
DGVUsers.Columns["USRStatus"]. Visible=false;
And then I used for to fill the value of every row depending on the value of the USRstatus value.
for (int i=0;i<DGVUsers.RowCount;i++)
{
If(Convert.ToByte(DGVUsers.Rows[i].Cells["USRStatus"].Value==1)
DGVUsers.Rows[i].Cells["status "].Value=Pharma.Propreties.Recourcess.on;
Else
DGVUsers.Rows[i].Cells["status"].Value=Pharma.Propreties.Recourcess.off;
}
It works fine but I want to do it by replacing the whole column, and not creating a new one depending on the other one if that's possible
You can use the DataGridViewImageColumn in the DataGridView. When you create a new column just choose the type DataGridViewImageColumn. When you want to change the image just set the value.
if ()
DataGridView.Rows[0].Cells[0].Value = Pharm.Propreties.Recourses.on;
else
DataGridView.Rows[0].Cells[0].Value = Pharm.Propreties.Recourses.off;
To see if it is on or off you can use the equals method (every type has this):
if (DataGridView.Rows[0].Cells[0].Value.Equals(Pharm.Properties.Resources.on)
{ }
Another way to do this is to use the Tag variable (every winforms control has this - System.Windows.Forms.Control):
DataGridView.Rows[0].Cells[0].Tag = true;
Then, to get its value:
if (DataGridView.Rows[0].Cells.Tag as bool)
{ }
or
if ((bool)DataGridView.Rows[0].Cells.Tag)
{ }
Personally, I like the as bool option more, it is more clear and esthetic.
Another way is to create a new control, but it is way more complicated for this purpose.
Good Luck!

Enabling and disabling of columns in Infragistics UltraGrid

I have an Infragistics grid and I want to disable and enable some columns based upon some requirement. I have read some articles that say to use AllowUpdate = DefaultableBoolean.True but it did not work for me.
I suppose that when you talk of disabled columns you mean disable editing in these columns.
Also you don't specify the language, so I will use C#
UltraGridColumn c = grdWork.DisplayLayout.Bands[0].Columns["YourColumnName"];
c.CellActivation = Activation.NoEdit;
c.CellClickAction = CellClickAction.CellSelect;
The property CellActivation could also be set to Activation.Disabled or Activation.ActivateOnly.
The property CellClickAction allows to set an appropriate selection status for the cell clicked. You could use CellSelect or RowSelect. (This last one, to mimic the behavior of a ListBox)
As usual, the real difficulty is to find the correct property. Then Intellisense will give you a quick and fair explanation of the meaning of these values.
If you just want to show and hide the columns as needed then you can try the following.
UltraGrid myGrid = new UltraGrid();
//Bind to your data here
myGrid.DisplayLayout.Bands[0].Columns["ColumnName"].Hidden = true;

How do I remove an Excel Chart Legend Entry with C#?

I want to remove the legend entry for some, but not all, series in my Excel chart. From my experience it seems as if SeriesCollection.Item(index) and LegendEntries.Item(index) are not related. Given a series n how can I remove only the legend for that series?
I'm using Office Interop 2010 with Visual Studio 2010. This is easily accomplished via the GUI by selecting the legend entry, then right clicking and choosing "delete".
To delete a legend entry you have to know the index of the legend you want to delete. Unfortunately, there doesn't seem to be a relationship available through the interop api that exposes the relationship between legend and series. There is one hokey workaround however. To remove a legend for a specific series the approach that worked for me was to remove the legend immediately after adding the series. This is the only time that the legend index is known.
// .
// . code to add series
// .
// remove the legend entry for the newly added series
chart.Legend.LegendEntries(chart.Legend.LegendEntries().Count).Delete();
I know the original question was about Office Interop 2010, but this seems to be a good reference point for dynamically showing and hiding series in a chart too, so I'll add the following top in case it helps others.
If you want to show/hide a series on a chartobject on a worksheet in Office 2013 (in VBA at least; not sure about interop), you can do the following:
Worksheets("MySheetName").ChartObjects("MyChartName").Chart.FullSeriesCollection("MyLedendSeriesName").IsFiltered = false
This hides the series without removing it. Set it to true to show the series again.
I needed to remove the last two legend entries from a chart because they were fake series added to create a cross-hairs effect. One series made the vertical line and the other horizontal. After the legend entry was removed, the series remained on the chart, which is what I wanted. I used the code below:
Microsoft.Office.Interop.Excel.ChartObject chartObj = null;
Microsoft.Office.Interop.Excel.Chart chart = null;
Microsoft.Office.Interop.Excel.Legend legend = null;
Microsoft.Office.Interop.Excel.LegendEntries legendEntries = null;
Microsoft.Office.Interop.Excel.LegendEntry legendItem;
int legendEntryCount = 0;
chartObj = (Microsoft.Office.Interop.Excel.ChartObject) xlws.ChartObjects("Chart 1");
chart = chartObj.Chart;
legend = chart.Legend;
legendEntries = (Microsoft.Office.Interop.Excel.LegendEntries) chart.Legend.LegendEntries();
legendEntryCount = legendEntries.Count;
if (legendEntryCount > 2)
{
legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount);
legendItem.Delete();
legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount - 1);
legendItem.Delete();
}

.NET Charting Controls: Label values from data source

I know that you can use keywords with labels, but is there any way to use a specific value from my data set as a label? What I would like to do is have a column of my data table contain a custom label string, so that each X-axis value graphed would have a unique label, but it seems there is no way to connect a series to an element from my data source. Is there a workaround to this, or will I just have to manually add labels over the chart?
I believe you are looking for setting a label for your datasource which is different from the columns which derive the X and Y values.In that case you can do this in the following manner
Chart1.Series[0].Points.DataBind(data, "XValue", "YValue",
"ToolTip=mytooltip,Label=LabelColumnName");
Here you can specify the column name against Label which would be used to display the label, in the same fashion you could also set up the tooltip from a different property.
If this is not you are looking out then please update the question with more details.
EDIT
you can set the position of the label using CustomProperties of the series
Chart1.Series[0].CustomProperties = "LabelStyle=Left"; Check this

How to refresh DataGridView cells style

I have this simple piece of code on a windows form containing said DataGridView (dgvStatsTable) :
public void LoadStatsTable(DataTable statsTable)
{
dgvStatsTable.DataSource = statsTable;
var smallFont = new Font(dgvStatsTable.Font.FontFamily, dgvStatsTable.Font.Size * 0.67f);
dgvStatsTable.Rows[0].Cells[0].Style.Font = smallFont;
dgvStatsTable.InvalidateCell(0, 0);
//dgvStatsTable.Invalidate();
dgvStatsTable.Refresh();
}
Once that function has been called, my DataGridView contains the correct data to see.
However, that style change that I wanted is not showing (first cell in top-right corner has to have smaller text).
Why?
Is it because the table is set to a DataSource rather than building rows and columns?
Thanks!
The Solution to the problem was to write a handler for the DataGridView.CellFormatting Event
found in this MSDN article in the Setting Styles Dynamically section.
Here is a very nice answer from MSDN network, it appears that in order to have greater control you will need to override some functions.
http://msdn.microsoft.com/en-us/library/7fb61s43(VS.80).aspx

Categories

Resources