如何获取系列数据以在条形图图例中显示 [英] How do I get series data to display in bar chart legend
问题描述
我有一个Windows Forms应用程序,该应用程序显示来自数据库中存储的数据的图形.我能够将数据显示在条形图或饼形图中.但是条形图中的图例仅显示"Series1",即系列的名称.饼图的图例显示带有系列数据的正确图例.我搜索了MSDN,发现了几篇有关添加图例的文章,但是它们都具有相同的结果.
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.
这是我的条形图代码:
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);
这是我的饼图代码:
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);
我想念什么?请帮忙.
以下是条形图的输出: 条形图带有错误的图例
Here is the output of the bar chart: Bar Chart with bad Legend
这是饼图的输出: 具有良好图例的饼图
推荐答案
设计 的工作原理是这样的.
That is how Bar
and Pie
charts are designed to work.
所有ChartTypes
除外 Pie
图表在其Legend
中显示Series.Names
或SeriesTexts
.
All ChartTypes
except Pie
charts display the Series.Names
or the SeriesTexts
in their Legend
.
仅Pie
图表(仅具有一个 Series
)将显示DataPoint.YValues[0]
.
Only Pie
charts, which only have one Series
anyway will display the DataPoint.YValues[0]
.
如果您确实想在Legend
中显示数据点数据,则可以这样做,但是如果您添加多个数据点,则当然看起来很拥挤.
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..
这是如何添加隐藏常规Legend
并添加新的显示数据值的示例:
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);
}
如果需要,还可以将这些项目添加到常规Legend
中;只需创建一个引用并使用上面的代码即可:
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];
请注意,不过您无法从原始Legend
中删除原始项目!
Note that you can't delete the original items from the original Legend
, though!
这篇关于如何获取系列数据以在条形图图例中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!