I want to display in a chart, total values for 2 years grouped by month. What I tried is
C#
var sampleData = new List {
new Sample { Month = "Ianuarie", Value = Convert.ToInt32(row["M1"]) },
new Sample { Month = "Ianuarie", Value = Convert.ToInt32(row["L1"]) },
new Sample { Month = "Februarie", Value = Convert.ToInt32(row["M2"]) },
new Sample { Month = "Februarie", Value = Convert.ToInt32(row["L2"]) },
new Sample { Month = "Martie", Value = Convert.ToInt32(row["M3"]) },
new Sample { Month = "Martie", Value = Convert.ToInt32(row["L3"]) },
new Sample { Month = "Aprilie", Value = Convert.ToInt32(row["M4"]) },
new Sample { Month = "Aprilie", Value = Convert.ToInt32(row["L4"]) },
new Sample { Month = "Mai", Value = Convert.ToInt32(row["M5"]) },
new Sample { Month = "Mai", Value = Convert.ToInt32(row["L5"]) },
new Sample { Month = "Iunie", Value = Convert.ToInt32(row["M6"]) },
new Sample { Month = "Iunie", Value = Convert.ToInt32(row["L6"]) },
new Sample { Month = "Iulie", Value = Convert.ToInt32(row["M7"]) },
new Sample { Month = "Iulie", Value = Convert.ToInt32(row["L7"]) },
new Sample { Month = "August", Value = Convert.ToInt32(row["M8"]) },
new Sample { Month = "August", Value = Convert.ToInt32(row["L8"]) },
new Sample { Month = "Septembrie", Value = Convert.ToInt32(row["M9"]) },
new Sample { Month = "Septembrie", Value = Convert.ToInt32(row["L9"]) },
new Sample { Month = "Octombrie", Value = Convert.ToInt32(row["M10"]) },
new Sample { Month = "Octombrie", Value = Convert.ToInt32(row["L10"]) },
new Sample { Month = "Noiembrie", Value = Convert.ToInt32(row["M11"]) },
new Sample { Month = "Noiembrie", Value = Convert.ToInt32(row["L11"]) },
new Sample { Month = "Decembrie", Value = Convert.ToInt32(row["M12"]) },
new Sample { Month = "Decembrie", Value = Convert.ToInt32(row["L12"]) },
};
// data bind
Chart1.DataBindCrossTable(sampleData, "Month", "Month", "Value", "Label=Month");but the chart looks ugly. How do I make the XAxys show months, YAxis show counts in a month each year?



Marius VasileRank #785 · 2.9K reputationPosted Oct 20, 2024, 2:41 AM EDT
I managed to do it with
string[] xValues = {"Ianuarie", "Februarie","Martie","Aprilie","Mai","Iunie","Iulie","August","Septembrie","Octombrie","Noiembrie","Decembrie"}; int[] yValues1 = { Convert.ToInt32(row["M1"]), Convert.ToInt32(row["M2"]), Convert.ToInt32(row["M3"]), Convert.ToInt32(row["M4"]), Convert.ToInt32(row["M5"]), Convert.ToInt32(row["M6"]), Convert.ToInt32(row["M7"]), Convert.ToInt32(row["M8"]), Convert.ToInt32(row["M9"]), Convert.ToInt32(row["M10"]), Convert.ToInt32(row["M11"]), Convert.ToInt32(row["M12"]) }; int[] yValues2 = { Convert.ToInt32(row["L1"]), Convert.ToInt32(row["L2"]), Convert.ToInt32(row["L3"]), Convert.ToInt32(row["L4"]), Convert.ToInt32(row["L5"]), Convert.ToInt32(row["L6"]), Convert.ToInt32(row["L7"]), Convert.ToInt32(row["L8"]), Convert.ToInt32(row["L9"]), Convert.ToInt32(row["L10"]), Convert.ToInt32(row["L11"]), Convert.ToInt32(row["L12"]) }; // prima serie Series series1 = new Series("Series1") { IsValueShownAsLabel = true, ChartType = SeriesChartType.Column, Color = Color.Red, Font = new Font(Chart1.Font.Name, 10, FontStyle.Regular), }; series1.LegendText = currentYear.ToString(); series1.ChartType = SeriesChartType.Column; series1.Points.DataBindXY(xValues, yValues1); series1.ToolTip = currentYear + " #VALX, #VALY"; // Anul, luna si valoarea inregistrata // a doua serie Series series2 = new Series("Series2") { IsValueShownAsLabel = true, ChartType = SeriesChartType.Column, Color = Color.BlueViolet, Font = new Font(Chart1.Font.Name, 10, FontStyle.Regular), }; series2.LegendText = lastYear.ToString(); series2.ChartType = SeriesChartType.Column; series2.Points.DataBindXY(xValues, yValues2); series2.ToolTip = lastYear + " #VALX, #VALY"; // Anul, luna si valoarea inregistrata // Adiciona as séries ao gráfico Chart1.Series.Add(series1); Chart1.Series.Add(series2);