简体   繁体   English

C#在每个堆积的柱形图上绘制一条水平线

[英]C# Draw a horizontal line on each stacked column chart

Currently I need to develop a tool with chart as the major component. 当前,我需要开发一个以图表为主要组件的工具。 Chart control is also new for me. 图表控件对我来说也是新的。 I have do a lot of reading, researching to learn and understand the whole picture of the chart control. 我已经做了大量的阅读,研究以学习和理解图表控件的整体情况。

After all, I had stuck and question on how to draw a horizontal line (blue & red horizontal line) on stacked column chart as image shown below: 毕竟,我一直坚持并质疑如何在堆积柱形图上绘制水平线(蓝色和红色水平线),如下图所示:

这是我的项目的要求

Here what I have done so far: 这是我到目前为止所做的:

我的图表工具项目

This is my code so far: 到目前为止,这是我的代码:

            // X-Axis labels settings
        chart.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
        chart.ChartAreas[0].AxisX.Interval = 1;

        // Y-Axis labels settings
        //chart.ChartAreas[0].AxisY.Minimum = 100;
        chart.ChartAreas[0].AxisY.Minimum = 95;

        // Plotting chart
        using (YieldEntities context = new YieldEntities())
        {

            // Extract yield loss list
            var yeilds = (
                from yeild in context.YeildDatas
                group yeild by new { yeild.Loss } into newyeild
                select new
                {
                    Loss = newyeild.Key.Loss,
                    Percentage = newyeild.Sum(p => p.Percentage)
                }).OrderByDescending(p => p.Percentage);

            //context.YeildDatas.Select(p => new { p.Loss, Percentage = p }).Distinct();

            // Create new series
            foreach (var yield in yeilds)
            {
                chart.Series.Add(yield.Loss);
                chart.Series[yield.Loss].ChartType = SeriesChartType.StackedColumn100;
            }

            // Label settings for first series
            chart.Series[0].SmartLabelStyle.Enabled = false;
            chart.Series[0].LabelAngle = -90;
            chart.Series[0].Font = new Font(Font.FontFamily, 15, FontStyle.Bold);
            chart.Series[0].IsValueShownAsLabel = true;

            var query = context.YeildDatas.ToList();
            foreach (var item in query)
            {
                DataPoint dp = new DataPoint();
                dp.SetValueXY(item.DateString, item.Percentage);
                chart.Series[item.Loss].Points.Add(dp);
            }

            // Set empty datapoint for each series
            foreach (var yield in yeilds)
            {
                DataPoint nulldp = new DataPoint();
                nulldp.SetValueXY("", 0);
                chart.Series[yield.Loss].Points.Insert(1, nulldp);
                chart.Series[yield.Loss].Points.Insert(6, nulldp);
                chart.Series[yield.Loss].Points.Insert(11, nulldp);
            }

            chart.Legends["Legend"].IsEquallySpacedItems = true;
            chart.Legends["Legend"].IsTextAutoFit = true;

        }

I hope to get any expert to guide me to solve this problem. 我希望有任何专家来指导我解决这个问题。

This is only sample, you can start from there: 这仅是示例,您可以从此处开始:

// Data point to pixel
var pixelX = this.chart1.ChartAreas[0].AxisX.ValueToPixelPosition(dataPointX);
var pixelY = this.chart1.ChartAreas[0].AxisY.ValueToPixelPosition(dataPointY);

// Pixel to data point
var dataPointX = this.chart1.ChartAreas[0].AxisX.PixelPositionToValue(pixelX);
var dataPointY = this.chart1.ChartAreas[0].AxisY.PixelPositionToValue(pixelY);

// Use event Paint to draw your line
private void chart1_Paint(object sender, PaintEventArgs e)
{
  // Convert dataPoint to pixel
  var dataPointX = this.chart1.Series[0].Points[0].XValue;
  var dataPointY = this.chart1.Series[0].Points[0].YValues[0];

  var pixelX = this.chart1.ChartAreas[0].AxisX.ValueToPixelPosition(dataPointX);
  var pixelY = this.chart1.ChartAreas[0].AxisY.ValueToPixelPosition(dataPointY);

  // Only sample, pen should be initialized outside Paint method
  Pen pen = new Pen(Brushes.Red);

  // Example of drawing line with width=100 pixel
  e.Graphics.DrawLine(pen, pixelX, pixelY, pixelX + 100, pixelY);  
}

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

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