How to control winform mschart legend text alignment c#? - c#

How does one set the alignment of text within a chart legend object? I've tried using:
myChartName.Legends["mySeriesName"].Alignment = stringAlignment.Near
With no effect. I've also tried to create custom legend items, again resulting in no effect. Text is ALWAYS centered (along with the series marker) in the legend "box". The only text I have been able to align is the title, but I don't need titles in my application.
My research to date says the legend object is basically a table with (by default) two cells. If that is the case there should be a way to access those cells and manipulate them as table cells. So, what gives? Why can't I access the text alignment properties of the legend object? Clearly, there is something I'm missing, but for the life of me I can't seem to figure this out. Quite frustrating.

Problem solved. The CustomItem approach wasn't working either, so I tried using the LegendCellColumn Class.
I changed the LegendStyle from Column to Row, then added two CellColumns, one for the series symbol and one for the legend text. Set the alignment, margins, and column widths (that turned out to be the trick), and voila; a legend that looks like I want. Here's the code for anyone with a similar issue.
chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.SeriesSymbol, ""));
chartSel.Legends[ySeries.Name].CellColumns[0].Alignment = ContentAlignment.TopLeft;
chartSel.Legends[ySeries.Name].CellColumns[0].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1);
chartSel.Legends[ySeries.Name].CellColumns[0].MinimumWidth = 250;
chartSel.Legends[ySeries.Name].CellColumns[0].MaximumWidth = 250;
chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, ySeries.Name));
chartSel.Legends[ySeries.Name].CellColumns[1].Alignment = ContentAlignment.MiddleLeft;
chartSel.Legends[ySeries.Name].CellColumns[1].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1);
chartSel.Legends[ySeries.Name].CellColumns[1].MinimumWidth = 1500;
chartSel.Legends[ySeries.Name].CellColumns[1].MaximumWidth = 1500;
It's probably not the most efficient way to do it, but it works. Technically, the legend symbol and text are still centered in the object, but because I'm forcing the widths of the two columns it has the appearance of being left-justified.
Hopefully, this may help another newbie like me avoid days of consternation.

My understanding is that Legend alignment relates to the Docking property, not really how the text is aligned within the legend. So setting Alignment to Near means positioning the legend box near the Docking direction.
It is quite hard to explain this textually. The MS Chart Samples have a subsection named Chart features -> Legends -> Style and Auto Positioning which will help you play with those two properties and understand how they are meant to work.
For inner legend alignment, you may need to use Legend.CustomItems and define LegendCell individually.
chart.Legends["Default"].CustomItems.Clear();
chart.Legends["Default"].CustomItems.Add(new LegendItem("LegendItem", Color.Red, ""));
chart.Legends["Default"].CustomItems[0].Cells.Add(new LegendCell(LegendCellType.Text, "My text", ContentAlignment.MiddleLeft));
This is described inside the Chart features -> Legends -> Legend Cells section of the samples.

While continuing to try to figure this out I stumbled on this tidbit of info from the following MSDN library page:
http://msdn.microsoft.com/en-us/library/dd456711(v=vs.100).aspx
"NOTE: You cannot adjust the individual legend items and cells in the Chart.Legends collection. To adjust them, use custom legend items."
So, back to the CustomItem route. I've got this code gleaned from several sources (including you, Dominique, thanks):
chartSel.Legends.Add(ySeries.Name);
chartSel.Series[ySeries.Name].IsVisibleInLegend = false;
chartSel.Legends[ySeries.Name].CustomItems.Clear();
LegendItem li = new LegendItem();
li.ImageStyle = LegendImageStyle.Line;
li.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.TopLeft);
li.Cells[0].BackColor = Color.CornflowerBlue; //color is only to see the cell bounds
li.Cells.Add(LegendCellType.Text, ySeries.Name, ContentAlignment.TopLeft);
li.Cells[1].BackColor = Color.Aquamarine; //color is only to see the cell bounds
chartSel.Legends[ySeries.Name].CustomItems.Add(li);
From what I can find it should work, but it doesn't. It results in a legend object with two cells; the first is empty and the second has left-justified text. I tried to post an image of it, but the system says I'm not yet worthy.
Why there is no line for the series symbol is also a mystery, but that's another problem to solve. The text justification issue is my main concern at the moment. It looks like the text within the cells is left-justified as desired, but the cells themselves are centered in the legend object; not what I wanted. I want those cells to be left-justified in the object too, so the first cell is against the left edge of the legend object.
Ideas?

Related

How to make sure endlabels are always visible on c# chart x axis

I have created a chart in the image below and I need to insert in addition to the existing labels, the end labels on the extreme left and right of the x-axis. There are numerous datapoints and therefore I cannot set the interval to 1 for the labels as they'll clutter the axis. I have already tried setting the property
chartHistory.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true;
but it doesn't seem to work. How can I achieve this?
You may try:
chartHistory.ChartAreas[0].AxisX.IsMarginVisible = true;
chartHistory.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true;
Or probably you will have to add the labels yourself,
you can either use DataPoint.AxisLabel:
chartHistory.Series[0].Points[0].AxisLabel = "5/4/2010";
or more flexibly:
chartHistory.Series[0].Points[0].AxisLabel =
System.DateTime.FromOADate(chartHistory.Series[0].Points[0].XValue).ToShortDateString();
(and the same for the last point in series)
or you can add a custom label to the AxisX control:
chartHistory.ChartAreas[0].AxisX.CustomLabels.Add(0, 20, "5/4/2010");
See also this answer and this answer.

Draw a borderless table in iTextSharp

It appears as though the PDfPCell class does have a border property on it but not the PdfPTable class.
Is there some property on the PdfPTable class to set the borders of all its contained cells in one statement?
Borders are defined at the level of the cell, not at the level of the table. Hence: if you want to remove the borders of the table, you need to remove the borders of each cell.
By default, each cell has a border. You can change this default behavior by changing the border of each cell. For instance: if you create PdfPCell objects, you use:
cell.setBorder(Rectangle.NO_BORDER);
In case the cells are created internally, you need to change that property at the level of the default cell. See What is the PdfPTable.DefaultCell property used for?
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
For special borders, for instance borders with rounded corners or a single border for the whole table, or double borders, you can use either cell events or table events, or a combination of both. In Chapter 5 of my book "iText in Action - Second Edition", you'll find a PDF with double borders. See the PressPreviews example to see how this was done. Note that all examples from the book were ported to C#. You can find these examples here.
The official site also has examples where the borders are dotted lines, have rounded corners, and so on.
iTextSharp has no setBorder() method.
I tried the following:
cell.HasBorder(Rectangle.NO_BORDER); // <= this did nothing
cell.BorderColor = BaseColor.WHITE; // <= this works for me, but if your document's background is not white, you might not like it
cell.BorderWidth = 0; // <= this works like gangbusters
So it seems the "safest" way to go is to set BorderWidth to zilch AKA nada.
The following worked for me.
cell.Border = Rectangle.NO_BORDER;

ChartFX export chart cuts the borders

I have a problem with exporting a chart. I use the ChartFX chart with
chart.ExportImageSize = new Size(600, 450);
and if the size of the image is larger than this (1127, 537), it cuts the right and the bottom border in the exported image.
For exporting, I use simple
chart.Export(FileFormat.Bitmap);
No custom controls are used in exporting the chart, and the chart looks normal in the application (borders all around, and I use simple black border).
Few interesting things I realized trying to solve this.
First I have no border
chart.Border = new SimpleBorder(SimpleBorderType.None, cOffice2007BackColor);
Than, I add the new border object just to export the chart with the border.
chart.Border = new SimpleBorder(SimpleBorderType.Color, Color.Black);
chart.Export(FileFormat.Bitmap);
Than I revert the border. And, it exports the chart with the new border, but it doesn't resize the border. If it's larger than ExportImageSize, I see only left and top border, and if it's smaller, I get a part of the chart that goes outside the borders.
So, I set the the border to begin with, and only change the color for the exporting.
One more realization, explicitly setting the ExportImageSize can lead to some interesting side-effects. Even dough your plot looks really good, it sometimes cuts the legend, if it is to large

MS column chart showing arrows

Anybody can guide me why MS Column Chart is showing arrows. When I am displaying label values then start displaying it.
Please the below snap:
MSChart uses SmartLabel Technology to display labels.
There are two ways you can get rid of your lines.
Disable the SmartLabel Technology
series.SmartLabelStyle.Enabled = False;
But you may not like the end result as the labels might collide and your chart will become unreadable
Or you can selectively do this
s.SmartLabelStyle.CalloutStyle = LabelCalloutStyle.None;
s.SmartLabelStyle.CalloutLineAnchorCapStyle = LineAnchorCapStyle.None;
s.SmartLabelStyle.CalloutLineColor = Color.Transparent;
But again it might confuse end user and the label might not be near the correct datapoint/bar
More Information here
http://msdn.microsoft.com/en-us/library/system.web.ui.datavisualization.charting.smartlabelstyle.aspx
http://support2.dundas.com/OnlineDocumentation/WebChart2005/UsingSmartLabels.html
This line is enough for removing arrow sign in chart.
series.SmartLabelStyle.CalloutLineColor = Color.Transparent;
Check the below code its worked for me
chart1.Series[0].SmartLabelStyle.CalloutLineAnchorCapStyle = LineAnchorCapStyle.None;

how to change the direction of X-axis label in ms charts

Hi I am using Ms chart control in winforms application for displaying values according to dates
I need to change the x-axis label values(Dates) direction horizantal to vertical
I have searched so many properties but i did not find any solution for this.
Any one help me on this problem
Many Thanks ....
As I understand your question - you are asking how to rotate the chart label to display vertically.
You can rotate the x-axis label as follows:
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
This assumes you have associated your series with the first chart area, which is the default without modification when using the Winforms designer.
The following images shows how the chart would look before the code above is applied, the second image shows how it appears after the code is applied.
Let me know if this is not what you are trying to do and I will post an updated answer.
Before rotation
After Rotation
Edit: Another answer added after my initial post mentions in certain situations it may be important to set chartArea1.AxisX.IsLabelAutoFit = false;
If you have not already done so, get the chart samples from microsoft:
http://archive.msdn.microsoft.com/mschart
Then check the section on Labels
Chart Features > Labels
To answer your question directly, set the angle in LabelStyle, and don't forget to disable autofit
chartArea1.AxisX.IsLabelAutoFit = false;
chartArea1.AxisX.LabelStyle.Angle = 90;

Categories