itextsharp table chart legends beneath bars - c#

Can someone explain me how I can create chart like this one using itextsharp library.
As you can see here, this chart has attached table with legends to its bottom side. And each bar has year attached to it. I want to create something like that.
Second picture. This is what i have so far. I don't have year attached to each bar, and my legends are not beneath each bar like in top graph. If someone could guide me how to create identical graph like one on top i would be grateful. Thanks in advance!
How can I turn those labels to be displayed horizontally, and put those legends beneath years in table like one in picture number 1.
// Chart Centers By Year
var chartCentersByYear = new Chart
{
Width = 1000,
Height = 450,
RenderType = RenderType.ImageTag,
AntiAliasing = AntiAliasingStyles.Graphics,
TextAntiAliasingQuality = TextAntiAliasingQuality.High
};
chartCentersByYear.Titles.Add("Centers By Year");
chartCentersByYear.Titles[0].Font = new Font("Arial", 16f);
chartCentersByYear.Titles[0].Alignment = System.Drawing.ContentAlignment.TopLeft;
chartCentersByYear.ChartAreas.Add("");
chartCentersByYear.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chartCentersByYear.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
chartCentersByYear.Series.Add("Count");
chartCentersByYear.Series.Add("Cases");
chartCentersByYear.Series[0].ChartType = SeriesChartType.Column; //Pie
chartCentersByYear.Series[1].ChartType = SeriesChartType.StepLine; //StepLine
chartCentersByYear.Series[1].BorderDashStyle = ChartDashStyle.DashDot;
chartCentersByYear.Series[1].BorderWidth = 3;
chartCentersByYear.Series[0].Color = Color.Brown;
chartCentersByYear.Legends.Add("1");
chartCentersByYear.Legends.Add("2");
chartCentersByYear.Legends[0].HeaderSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[0].HeaderSeparatorColor = Color.Black;
chartCentersByYear.Legends[0].ItemColumnSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[0].ItemColumnSeparatorColor = Color.Black;
chartCentersByYear.Legends[1].HeaderSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[1].HeaderSeparatorColor = Color.Black;
chartCentersByYear.Legends[1].ItemColumnSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[1].ItemColumnSeparatorColor = Color.Black;
//For the Legend
LegendCellColumn firstColumn = new LegendCellColumn();
firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
firstColumn.HeaderBackColor = Color.WhiteSmoke;
chartCentersByYear.Legends[0].CellColumns.Add(firstColumn);
chartCentersByYear.Legends[1].CellColumns.Add(firstColumn);
LegendCellColumn secondColumn = new LegendCellColumn();
secondColumn.ColumnType = LegendCellColumnType.Text;
secondColumn.Text = "#LEGENDTEXT";
secondColumn.HeaderBackColor = Color.WhiteSmoke;
LegendItem newItemCount = new LegendItem();
newItemCount.Cells.Add(LegendCellType.Text, "Count", System.Drawing.ContentAlignment.MiddleCenter);
newItemCount.BorderWidth = 1;
newItemCount.BorderDashStyle = ChartDashStyle.Solid;
LegendItem newItemCases = new LegendItem();
newItemCases.Cells.Add(LegendCellType.Text, "Cases", System.Drawing.ContentAlignment.MiddleCenter);
newItemCases.BorderWidth = 1;
newItemCases.BorderDashStyle = ChartDashStyle.Solid;
// Getting data from a stored procedure
var totalCentersByYearResult = new Repository().GetTotalCentersByYear();
foreach (IGD_spInternationalReportCenterWithTots1_Result item in totalCentersByYearResult)
{
// For Series
chartCentersByYear.Series[0].Points.AddXY(item.YearEcmo, item.Count);
chartCentersByYear.Series[1].Points.AddY(item.Cases);
// For Legend
newItemCount.Cells.Add(LegendCellType.Text, item.Count.ToString(), System.Drawing.ContentAlignment.MiddleCenter);
newItemCases.Cells.Add(LegendCellType.Text, item.Cases.ToString(), System.Drawing.ContentAlignment.MiddleCenter);
}
chartCentersByYear.Legends[0].CustomItems.Add(newItemCount);
chartCentersByYear.Legends[0].CustomItems.Add(newItemCases);
chartCentersByYear.Legends[0].Docking = Docking.Bottom;
chartCentersByYear.Legends[1].Docking = Docking.Bottom; //Top
chartCentersByYear.Series[0].YAxisType = AxisType.Primary;
chartCentersByYear.Series[1].YAxisType = AxisType.Secondary;
//For two coordinate systems
chartCentersByYear.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
chartCentersByYear.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
chartCentersByYear.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
chartCentersByYear.ChartAreas[0].AxisY2.IsStartedFromZero = chartCentersByYear.ChartAreas[0].AxisY.IsStartedFromZero;
using (var chartimage = new MemoryStream())
{
chartCentersByYear.SaveImage(chartimage, ChartImageFormat.Png);
Byte[] newChart = chartimage.GetBuffer(); //return chartimage.GetBuffer();
var image = Image.GetInstance(newChart); //Image.GetInstance(Chart());
image.ScalePercent(50f);
image.SetAbsolutePosition(document.LeftMargin + 40, document.BottomMargin + 100);
document.Add(image);
}

Related

problem with save as image chart in spire.xls

I Used spire.xls to create a sheet and a chart but my problem is when I save a chart as a PDF it would be so inappropriate just need to justify the chart but don't know how I think it would be the problem with scale
Chart chart = sheet.Charts.Add(ExcelChartType.ScatterSmoothedLineMarkers);
//Set region of chart data
chart.DataRange = sheet.Range["A2:A" + (_state.NumberOfReads + 2).ToString()].AddCombinedRange(sheet.Range["F2:F" + (_state.NumberOfReads + 2).ToString()]);
chart.SeriesDataFromRange = false;
//Set position of the chart
chart.LeftColumn = 22;
chart.TopRow = 6;
chart.RightColumn = 32;
chart.BottomRow = 29;
//Set and format category axis title
chart.PrimaryCategoryAxis.Title = "Applied Pressure DUT.";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
//Set and format value axis title
chart.PrimaryValueAxis.Title = "Average of REF.";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = -90;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.Legend.Delete();
//Loop through the data series of the chart
foreach (ChartSerie cs in chart.Series)
{
cs.Format.Options.IsVaryColor = true;
//Show data labels for data points
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
}
chart.PrimaryValueAxis.HasMajorGridLines = true;
IChartTrendLine trendLine = chart.Series[0].TrendLines.Add(TrendLineType.Linear);
chart.Series[0].TrendLines[0].DisplayEquation = true;
chart.Series[0].TrendLines[0].DisplayRSquared = true;
//chart.HasDataTable = true;
Image[] imgs = workbook.SaveChartAsImage(sheet);
Bitmap img = new Bitmap(imgs[0]);
img.Save(path + filePic, ImageFormat.Png);
and also look at the outputs

How to add labels both inside and outside chart

I want to add labels both inside and outside of a pie chart.
I have tried adding values to it, it works either inside or outside.
I want to display both VALX and VALY on my chart
How can I do this?
I want my chart to appear like this.
XmlNodeList xnList = xml.SelectNodes("/Report/Parameters/Parameter");
var chart = new Chart();
chart.Height = 600;
chart.Width = 900;
ArrayList xAxisData = new ArrayList();
ArrayList yAxisData = new ArrayList();
string title = "Motility";
chart.Titles.Add(title);
var chartArea1 = new ChartArea();
chart.ChartAreas.Add(chartArea1);
chart.ChartAreas[0].AlignmentStyle = AreaAlignmentStyles.All;
Series series1;
xAxisData.Clear();
yAxisData.Clear();
string seriesName1 = " ";
byte ColorIndex = 0;
series1 = new Series();
seriesName1 = "Pie Chart";
series1.Name = seriesName1;
series1.ChartType = SeriesChartType.Pie;
chart.Series.Add(series1);
chart.ChartAreas[0].Area3DStyle.Enable3D = true;
chart.ChartAreas[0].Area3DStyle.Inclination = 40;
chart.ChartAreas[0].BorderColor = Color.Black;
chart.ChartAreas[0].Area3DStyle.WallWidth = 20;
chart.Series[seriesName1]["3DLabelLineSize"] = "30";
chart.Series[seriesName1].IsVisibleInLegend = false;
foreach (KeyValuePair<string, ReportParameter> pair in T_NewChart.parameters)
{
chart.Series[seriesName1].Points.AddXY((pair.Value.Name), pair.Value.Values[0]));
if (ColorIndex == 0)
{
chart.Series[seriesName1].Points[0].Color = Color.Green;
chart.Series[seriesName1].Label = "#VALX";
chart.Series[seriesName1]["PieLabelStyle"] = "Outside";
chart.Series[seriesName1].BorderColor = Color.Black;
chart.Series[seriesName1].Points[0].Label = "Total";
chart.Series[0].Font = new System.Drawing.Font("Arial", 15F);
}
else if (ColorIndex == 1)
{
chart.Series[seriesName1].Points[1].Color = Color.Yellow;
chart.Series[seriesName1].Points[1].Label = "Non";
}
else if (ColorIndex == 2)
{
chart.Series[seriesName1].Points[2].Color = Color.Red;
chart.Series[seriesName1].Points[2].Label = "Few";
}
ColorIndex++;
}
chart.SaveImage("C:\\NewPie_chart.png", ChartImageFormat.Png);
I can only display the "Name", but I want to display the "Name" (VALX) outside the chart and the "Value" (VALY) inside the chart.

How to set chart title above y-axis?

I have been experimenting with charts and so far I got this experimental code:
static void Main(string[] args)
{
Random rnd = new Random();
Chart thisChart = new Chart();
thisChart.Height = 400;
thisChart.Width = 500;
thisChart.BackColor = SystemColors.Window;
thisChart.Palette = ChartColorPalette.EarthTones;
thisChart.Titles.Add("Test");
thisChart.Visible = true;
ChartArea ca = new ChartArea();
ca.Name = "Default";
ca.BackColor = Color.White;
ca.BorderColor = Color.FromArgb(26, 59, 105);
ca.BorderWidth = 0;
ca.BorderDashStyle = ChartDashStyle.Solid;
ca.AxisX = new Axis();
ca.AxisY = new Axis();
thisChart.ChartAreas.Add(ca);
Series series = thisChart.Series.Add("Default");
series.ChartType = SeriesChartType.Spline;
series.ChartArea = "Default";
series.Points.AddXY("1", 20);
series.Points.AddXY("2", 25);
series.Points.AddXY("3", 30);
series.Points.AddXY("4", 35);
series.Points.AddXY("5", 40);
series.Points.AddXY("6", 45);
//SetPosition for multiple Y-axis labels
thisChart.ChartAreas["Default"].Position = new ElementPosition(25, 10, 68, 85);
thisChart.ChartAreas["Default"].InnerPlotPosition = new ElementPosition(10, 0, 90, 80);
// Create extra Y axis for second and third series
Series series2 = thisChart.Series.Add("Series2");
series2.ChartType = SeriesChartType.Spline;
series2.ChartArea = "Default";
series2.Points.AddXY("1", 50);
series2.Points.AddXY("2", 55);
series2.Points.AddXY("3", 60);
series2.Points.AddXY("4", 70);
series2.Points.AddXY("5", 75);
series2.Points.AddXY("6", 80);
//series2.Legend = "Default";
thisChart.Series["Series2"].ChartArea = "Default";
CreateYAxis(thisChart, thisChart.ChartAreas["Default"], thisChart.Series["Series2"], 13, 8);
// Create extra X axis for second and third series
Series series3 = thisChart.Series.Add("Series3");
series3.ChartType = SeriesChartType.Spline;
series3.ChartArea = "Default";
series3.Points.AddXY("1,5", 75);
series3.Points.AddXY("2,5", 175);
series3.Points.AddXY("3,5", 300);
series3.Points.AddXY("4,5", 75);
series3.Points.AddXY("5,5", 150);
series3.Points.AddXY("6,6", 125);
thisChart.Series["Series3"].ChartArea = "Default";
CreateXAxis(thisChart, thisChart.ChartAreas["Default"], thisChart.Series["Series3"], -10, 8);
//thisChart.DataBind();
thisChart.SaveImage(#"C:\Temp\TestChart.png", ChartImageFormat.Png);
}
private static void CreateYAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
{
// Create new chart area for original series
ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
areaSeries.BackColor = Color.Transparent;
areaSeries.BorderColor = Color.Transparent;
areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
areaSeries.AxisX.MajorGrid.Enabled = false;
areaSeries.AxisX.MajorTickMark.Enabled = false;
areaSeries.AxisX.LabelStyle.Enabled = false;
areaSeries.AxisY.MajorGrid.Enabled = false;
areaSeries.AxisY.MajorTickMark.Enabled = false;
areaSeries.AxisY.LabelStyle.Enabled = false;
areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
areaSeries.AxisY.TitleAlignment = StringAlignment.Far;
areaSeries.AxisY.TextOrientation = TextOrientation.Horizontal;
areaSeries.AxisY.Title = "Y-axis Title1";
areaSeries.AxisY.TitleForeColor = Color.Blue;
areaSeries.AxisY.TitleFont = new Font("Tahoma", 7, FontStyle.Bold);
areaSeries.AxisY.Maximum = 300;
series.ChartArea = areaSeries.Name;
// Create new chart area for axis
ChartArea areaAxis = chart.ChartAreas.Add("AxisY_" + series.ChartArea);
areaAxis.BackColor = Color.Transparent;
areaAxis.BorderColor = Color.Transparent;
areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());
areaAxis.AxisY.TitleAlignment = StringAlignment.Center;
areaAxis.AxisY.Title = "Y-axis Title";
areaAxis.AxisY.TitleForeColor = Color.Blue;
areaAxis.AxisY.TitleFont = new Font("Tahoma", 7, FontStyle.Bold);
areaAxis.AxisY.TitleAlignment = StringAlignment.Far;
areaAxis.AxisY.TextOrientation = TextOrientation.Horizontal;
areaAxis.AxisY.Maximum = 200;
// Create a copy of specified series
Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
seriesCopy.ChartType = series.ChartType;
foreach (DataPoint point in series.Points)
{
seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
}
// Hide copied series
seriesCopy.IsVisibleInLegend = false;
seriesCopy.Color = Color.Transparent;
seriesCopy.BorderColor = Color.Transparent;
seriesCopy.ChartArea = areaAxis.Name;
// Disable drid lines & tickmarks
areaAxis.AxisX.LineWidth = 0;
areaAxis.AxisX.MajorGrid.Enabled = false;
areaAxis.AxisX.MajorTickMark.Enabled = false;
areaAxis.AxisX.LabelStyle.Enabled = false;
areaAxis.AxisY.MajorGrid.Enabled = false;
areaAxis.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
areaAxis.AxisY.LabelStyle.Font = area.AxisY.LabelStyle.Font;
// Adjust area position
areaAxis.Position.X -= axisOffset;
areaAxis.InnerPlotPosition.X += labelsSize;
}
private static void CreateXAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
{
// Create new chart area for original series
ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
areaSeries.BackColor = Color.Transparent;
areaSeries.BorderColor = Color.Transparent;
areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
areaSeries.AxisY.MajorGrid.Enabled = false;
areaSeries.AxisY.MajorTickMark.Enabled = false;
areaSeries.AxisY.LabelStyle.Enabled = false;
areaSeries.AxisX.MajorGrid.Enabled = false;
areaSeries.AxisX.MajorTickMark.Enabled = false;
areaSeries.AxisX.LabelStyle.Enabled = false;
areaSeries.AxisX.IsStartedFromZero = area.AxisX.IsStartedFromZero;
series.ChartArea = areaSeries.Name;
// Create new chart area for axis
ChartArea areaAxis = chart.ChartAreas.Add("AxisX_" + series.ChartArea);
areaAxis.BackColor = Color.Transparent;
areaAxis.BorderColor = Color.Transparent;
areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());
// Create a copy of specified series
Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
seriesCopy.ChartType = series.ChartType;
foreach (DataPoint point in series.Points)
{
seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
}
// Hide copied series
seriesCopy.IsVisibleInLegend = false;
seriesCopy.Color = Color.Transparent;
seriesCopy.BorderColor = Color.Transparent;
seriesCopy.ChartArea = areaAxis.Name;
// Disable drid lines & tickmarks
areaAxis.AxisY.LineWidth = 0;
areaAxis.AxisY.MajorGrid.Enabled = false;
areaAxis.AxisY.MajorTickMark.Enabled = false;
areaAxis.AxisY.LabelStyle.Enabled = false;
areaAxis.AxisX.MajorGrid.Enabled = false;
//areaAxis.AxisX.IntervalOffset = Convert.ToDouble("0,5");
areaAxis.AxisX.IsStartedFromZero = area.AxisX.IsStartedFromZero;
areaAxis.AxisX.LabelStyle.Font = area.AxisX.LabelStyle.Font;
areaAxis.AxisX.CustomLabels.Add(0, 1, "0.5");
areaAxis.AxisX.CustomLabels.Add(1, 2, "1.5");
areaAxis.AxisX.CustomLabels.Add(2, 3, "2.5");
areaAxis.AxisX.CustomLabels.Add(3, 4, "3.5");
areaAxis.AxisX.CustomLabels.Add(4, 5, "4.5");
areaAxis.AxisX.CustomLabels.Add(5, 6, "5.5");
areaAxis.AxisX.CustomLabels.Add(6, 7, "6.5");
// Adjust area position
areaAxis.Position.Y -= axisOffset;
areaAxis.InnerPlotPosition.Y += labelsSize;
}
This produces the following result:
PROBLEM:
I can not figure out how to make it so that the title aligns itself above the respective y-axis, any ideas?
I don't think you do that.
Afaik the recommended workaround is adding more chart Titles.
You can style them as usual and to move them on top of a axis you can align them to the top left of the respective Chartearea.InnerPlotPosition:
ChartArea ca1 = thisChart.ChartAreas[0];
RectangleF rip1 = ca1.InnerPlotPosition.ToRectangleF();
Title ty1 = thisChart.Titles.Add("ty1");
ty1.Text = "Y-Axis 1\nTitle";
ty1.ForeColor = Color.DarkSlateBlue;
ty1.Position.X = rip1.Left;
ty1.Position.Y = rip1.Y;
Make sure to have enough space at the top of the chart for the titles..
Do note that the values of all ElementPositions, including InnerPlotPosition are in percent of the respective containers, i.e. of the ChartArea for the InnerPlotPosition and of the Chart for the ChartArea..

How do I get series data to display in bar chart legend

I have a Windows Forms Application that displays a graph from data stored in a database. I am able to get the data to display in a bar graph or pie graph. But the legend in the bar graph only displays "Series1", the name of the series. The legend of the pie chart displays a correct legend with the series data. I've searched MSDN and found several articles on adding a legend but they all have the same results.
Here is my bar chart code:
string[] xvals = new string[dt.Rows.Count];
int[] yvals = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
xvals[i] = dt.Rows[i]["XValues"].ToString();
yvals[i] = Convert.ToInt32(dt.Rows[i]["YValues"].ToString());
}
Chart barChart = new Chart();
ChartArea chartArea = new ChartArea();
barChart.ChartAreas.Add(chartArea);
barChart.Dock = DockStyle.Fill;
barChart.BackColor = Color.Transparent;
barChart.Palette = ChartColorPalette.Fire;
barChart.ChartAreas[0].BackColor = Color.Transparent;
barChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
barChart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
Series series1 = new Series
{ Name = "Series1", IsVisibleInLegend = true, ChartType = SeriesChartType.Bar };
series1.ChartType = SeriesChartType.Column;
barChart.Series.Add(series1);
for (int i = 0; i < dt.Rows.Count; i++)
{
series1.Points.AddXY(dt.Rows[i]["XValues"].ToString(),
Convert.ToInt32(dt.Rows[i]["YValues"].ToString()));
var p1 = series1.Points[i];
p1.Color = Color.FromArgb((byte)r.Next(90, 255), (byte)r.Next(90, 255), 160);
}
barChart.Legends.Add(new Legend("Legend1"));
barChart.Legends["Legend1"].BackColor = Color.Transparent;
barChart.Series["Series1"].Legend = "Legend1";
series1.IsVisibleInLegend = true;
gbo1.Controls.Add(barChart);
Here is my pie chart code:
string[] xvals = new string[dt.Rows.Count];
int[] yvals = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
xvals[i] = dt.Rows[i]["XValues"].ToString();
yvals[i] = Convert.ToInt32(dt.Rows[i]["YValues"].ToString());
}
Chart pieChart = new Chart();
ChartArea chartArea = new ChartArea();
chartArea.Name = "PieChartArea";
pieChart.ChartAreas.Add(chartArea);
pieChart.Dock = DockStyle.Fill;
pieChart.Location = new Point(0, 50);
pieChart.Palette = ChartColorPalette.Fire;
pieChart.BackColor = Color.Transparent;
pieChart.ChartAreas[0].BackColor = Color.Transparent;
Series series2 = new Series
{ Name = "Series2", IsVisibleInLegend = true, ChartType = SeriesChartType.Pie };
pieChart.Series.Add(series2);
for (int i = 0; i < dt.Rows.Count; i++)
{
series2.Points.Add((int)dt.Rows[i]["YValues"]);
var p2 = series2.Points[i];
p2.Color = Color.FromArgb((byte)r.Next(90, 255), (byte)r.Next(90, 255), 160);
p2.LegendText = dt.Rows[i]["XValues"].ToString();
}
pieChart.Legends.Add(new Legend("Legend2"));
pieChart.Legends["Legend2"].BackColor = Color.Transparent;
pieChart.Series["Series2"].Legend = "Legend2";
series2.IsVisibleInLegend = true;
gboReport1.Controls.Add(pieChart);
What am I missing? Please help.
Here is the output of the bar chart:
Bar Chart with bad Legend
Here is the output of the pie chart:
Pie Chart with good Legend
That is how Bar and Pie charts are designed to work.
All ChartTypes except Pie charts display the Series.Names or the SeriesTexts in their Legend.
Only Pie charts, which only have one Series anyway will display the DataPoint.YValues[0].
If you really want to display datapoint data in your Legend you can do that, but of course it will look crowded if you add more than a few data points..
This is an example of how you can add hide the regular Legend and add a new one which displays the data values:
chart1.ApplyPaletteColors();
chart1.Legends[0].Enabled = false;
Legend L2 = new Legend();
chart1.Legends.Add(L2);
L2.Docking = Docking.Right;
foreach (DataPoint dp in yourSeries.Points)
{
LegendItem LI = new LegendItem(dp.YValues[0].ToString("0.##"), dp.Color, "");
LI.BorderWidth = 0;
L2.CustomItems.Add(LI);
}
If you want to you can also add those items to the regular Legend; simply create a reference to it and use the code above:
Legend L1 = chart1.Legends[0];
Note that you can't delete the original items from the original Legend, though!

MS Chart: X-AXIS labels repeating

I am trying to create multi series line chart using MS Chart. I have created it successfully. But the problem is the x-axis labels repeation. Here is what is created
Can anyone tell me why the months are repeated? how can i avoid it?
UPDATE:
Here is the code:
DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
decimal[] gvals = {4.3,0};
decimal[] ypvals = {0,0};
decimal[] yvals = {3.5,0};
// create the chart
var chart = new Chart();
chart.Size = new Size(600, 250);
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
chart.BorderlineWidth = 3;
var chartArea = new ChartArea();
chartArea.AxisX.MajorGrid.LineWidth = 0;
//Remove Y-axis grid lines
chartArea.AxisY.MajorGrid.LineWidth = 0;
chartArea.AxisX.LabelStyle.Format = "MMM";
chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chart.ChartAreas.Add(chartArea);
var series = new Series();
series.Name = "Y";
series.Legend = "Y";
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.DateTime;
series.IsVisibleInLegend = true;
series.Color = Color.Red;
series.IsValueShownAsLabel = true;
series.BorderWidth = 2;
chart.Series.Add(series);
// bind the datapoints
chart.Series[0].Points.DataBindXY(xvals, yvals);
var series2 = new Series();
series2.Name = "YP";
series2.Legend = "YP";
series2.ChartType = SeriesChartType.Line;
series2.XValueType = ChartValueType.DateTime;
series2.IsVisibleInLegend = true;
series2.Color = Color.Yellow;
series2.IsValueShownAsLabel = true;
series2.BorderWidth = 2;
chart.Series.Add(series2);
// bind the datapoints
chart.Series[1].Points.DataBindXY(xvals, ypvals);
var series3 = new Series();
series3.Name = "G";
series3.Legend = "GG";
series3.ChartType = SeriesChartType.Line;
series3.XValueType = ChartValueType.DateTime;
series3.IsVisibleInLegend = true;
series3.Color = Color.Blue;
series3.IsValueShownAsLabel = true;
series3.BorderWidth = 2;
chart.Series.Add(series3);
// bind the datapoints
chart.Series[2].Points.DataBindXY(xvals, gvals);
// draw!
chart.Invalidate();
// write out a file
chart.SaveImage("D:\\cha.png", ChartImageFormat.Png);
Ok, I have got the solution.
I have just converted the
series1.XValueType = ChartValueType.Date;
series2.XValueType = ChartValueType.Date;
series3.XValueType = ChartValueType.Date;
TO:
series1.XValueType = ChartValueType.String;
series2.XValueType = ChartValueType.String;
series3.XValueType = ChartValueType.String;
and instead of using dates in the xAxisValues array used months name as string.
DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
TO:
string[] xvals = {DateTime.Now.AddMonth(-1).ToString("MMM"),DateTime.Now.ToString("MMM")};
Hope this helps someone else.

Categories