简体   繁体   English

柱形图Microsoft图表控件的y轴上的百分比值

[英]Percent value in y axis of Column Chart Microsoft chart control

I am trying to get Column charts, where I need to have percentage value in y axis and should recalculate and scale. 我正在尝试获取柱形图,我需要在y轴上具有百分比值,并且应该重新计算和缩放。

I have seen some suggestion to assign minimum and maximum value ( chart.ChartAreas[0].AxisY.Minimum=0 ) but it's not adjusting the column height according to the percentage. 我看到了一些建议分配最小值和最大值( chart.ChartAreas[0].AxisY.Minimum=0 ),但是它没有根据百分比调整列高。 Any help will be appreciated. 任何帮助将不胜感激。

Below is what I have done so far 以下是我到目前为止所做的

 foreach (var value in labels)
   {

     chart.Legends[value].Alignment = StringAlignment.Center;
     chart.Legends[value].Docking = Docking.Bottom;
     chart.Series[value].ChartType = SeriesChartType.Column;
     chart.Series[value].IsValueShownAsLabel = true;
     chart.Series[value].Label = "#PERCENT{P0}";

     chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
     chart.ChartAreas[0].AxisY.MajorGrid.Enabled =false;
     chart.ChartAreas[0].AxisY.Minimum=0;
      // chart.ChartAreas[0].RecalculateAxesScale();

      chart.BringToFront();

      if (count == 0 && comp.Value != null)
        chart.Series[value].Points.Add(comp.Value[0]);
      else if (count >= 1 && comp.Value != null && comp.Value.Count() > count)
        chart.Series[value].Points.Add(comp.Value[count]);
      else
        chart.Series[value].Points.Add(0);

          count++;
}

柱形图 The Y axis should show the percentage and the columns height should be adjusted to the y axis percentage value. Y轴应显示百分比,列高度应调整为y轴百分比值。

Here is an example that shows all sorts of info about the data in the chart: 这是一个示例,显示有关图表中数据的各种信息:

  • The X&Y-Values in a ToolTip ToolTip的X&Y值
  • The percentages of the the values against the total on the Columns Columns值相对于总数的百分比
  • The percentage against the maximum at the Y-Axis 相对于Y-Axis最大值的百分比

在此处输入图片说明

Series S = chart1.Series[0];
ChartArea CA = chart1.ChartAreas[0];
Axis AY = CA.AxisY;

S.Points.AddXY(1, 10);      S.Points.AddXY(2, 40);
S.Points.AddXY(3, 50);      S.Points.AddXY(4, 100);
S.Points.AddXY(5, 111);  

S.IsValueShownAsLabel = true;
S.Label = "#PERCENT{P0}";

S.ToolTip = "#VALX{#.##}" + " : " + "#VALY1{#.##}";

double max = S.Points.Max(x => x.YValues[0]);

for (int i = 0; i < S.Points.Count; i++)
{
    DataPoint dp =  S.Points[i];
    double y0 = S.Points[i].YValues[0];
    AY.CustomLabels.Add(y0, y0 + 1, (y0 / max * 100f).ToString("0.0") + "%");
}

Of course you can change it all around as you please.. 当然,您可以随意更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM