How to line up windows C# charts - c#

Link to how it looks like http://puu.sh/hc4aJ/1cb4c189b0.png
I have multiple charts in my form and they can have different Y-axis labels. But when I add those different axis labels this happens. I haven't found any option to set a margin or something so that the graphs will be aligned.
Every graph has the same location and size settings.
chart.Size = new Size(1000, 190);
chart.Location = new Point(0, 0);
Anybody got a solution?

If all chart areas are in the same chart, you can use the AlignWithChartArea-Property.
chartArea1.AlignWithChartArea = "ChartArea2";

Related

Microsoft Chart Controls - Line Series Aliasing

I am using the Microsoft Chart Controls Chart class in System.Web.UI.DataVisualization.Charting and have a problem with a line series looking very jagged.
I am setting up my chart like so:
chart.AntiAliasing = AntiAliasingStyles.All;
chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
chart.BackColor = Color.White;
And my series is a basic SeriesChartType.Line type. I am rendering as a PNG (have also tried BMP and JPG), but the end result it still like the below. I have also tried increasing the line width, but this just makes it more obvious. Is it possible to smooth out the line?

Align two chart areas windows forms

I have a windows forms application that displays a chart compose by two chart areas, one for price/dates and the second displays volume/price, both should get the same amount of datapoints to draw in each chart area, the issue I have at the moment is that both charts are not aligned vertically so they are not very clear for the user, I added the following properties to volumen chart area:
volumeChartArea.AlignWithChartArea = CHART_AREA_PRICES;
volumeChartArea.AlignmentStyle = AreaAlignmentStyles.All;
volumeChartArea.AlignmentOrientation = AreaAlignmentOrientations.Vertical;
But they still don't look correct, what could it be the solution to fix this issue?
Many thanks in advance.
something like this maybe?
if (this.chrtMain.ChartAreas.Count > 0)
{
ca.AlignmentOrientation = AreaAlignmentOrientations.Vertical;
ca.AlignWithChartArea = this.chrtMain.ChartAreas[0].Name;
}
All chart areas will be aligned as they are added this way.

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

Adding a logo image to a chart using Microsoft Chart Control

I have a chart that is built using Microsoft Chart Controls and would like to add our company's logo to the corner. Is this possible to do with MCC?
Is there any reason you don't want to simply put a PictureBox control near the corner of the chart? That seems like that simplest solution to me.
To add an image, use ImageAnnotation.
ImageAnnotation logo = new ImageAnnotation();
logo.X = 75;
logo.Y = 60;
logo.Image = "imagePath";
chart.Annotations.Add(logo);

How to control winform mschart legend text alignment 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?

Categories