2016-11-08 97 views
-1

我有一个Windows窗体应用程序,用于显示存储在数据库中的数据的图形。我能够将数据显示在条形图或饼图中。但是条形图中的图例只显示系列的名称“Series1”。饼图的图例显示了系列数据的正确图例。我搜索了MSDN,发现了几篇关于添加图例的文章,但它们都有相同的结果。如何获取系列数据以条形图图例显示

这里是我的条形图代码:

 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); 

我缺少什么?请帮忙。

这里是条形图的输出: Bar Chart with bad Legend

下面是饼图的输出: Pie Chart with good Legend

+0

您使用哪种图表API? –

+0

这是你的意思:使用System.Windows.Forms.DataVisualization.Charting; –

回答

0

也就是说BarPie图表如何设计工作。

所有ChartTypes除了Pie图表显示Series.Names或在自己的LegendSeriesTexts

只有Pie图表,其中只有一个Series反正会显示DataPoint.YValues[0]

如果你真的想在显示数据点数据的Legend你能做到这一点,当然会显得拥挤,如果你不是几个数据点添加更多..

这是你怎么能一个例子添加隐藏定期Legend,并添加一个新的,显示的数据值:

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); 
} 

enter image description here

如果你愿意,你也可以这些项目添加到正规Legend;只需创建对它的引用,并使用上面的代码:

Legend L1 = chart1.Legends[0]; 

请注意,你不能从原来的Legend删除原来的项目,但!