C#Winforms-创建具有多个Y轴(3个或更多)的图表 [英] C# Winforms - Creating a chart with multiple Y axis (3 or more)

查看:813
本文介绍了C#Winforms-创建具有多个Y轴(3个或更多)的图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户需要使用两个以上Y轴的图表。

I have a client that needs to use charts with more than 2 Y axes.

我已经在使用具有内置Y2的组件一(C1Chart)图表轴及其工作

I am already using Component One (C1Chart) chart that has a built in Y2 axis and its working

很棒。

有人知道图表控件可以显示3个或更多Y图表上的坐标轴?

Does anyone knows of a chart control that can display 3 or more Y axes on a chart ?

推荐答案

Microsoft图表控件的示例环境 包含多个Y轴的示例。

一些代码段:

Samples Environments for Microsoft Chart Controls contains example of multiple Y axis.
Some pieces of code:

    private void checkBoxUseMultipleYAxis_CheckedChanged(object sender, System.EventArgs e)
    {
        if(checkBoxUseMultipleYAxis.Checked)
        {
            // Set custom chart area position
            Chart1.ChartAreas["Default"].Position = new ElementPosition(25,10,68,85);
            Chart1.ChartAreas["Default"].InnerPlotPosition = new ElementPosition(10,0,90,90);

            // Create extra Y axis for second and third series
            CreateYAxis(Chart1, Chart1.ChartAreas["Default"], Chart1.Series["Series2"], 13, 8);
            CreateYAxis(Chart1, Chart1.ChartAreas["Default"], Chart1.Series["Series3"], 22, 8);
        }
        else
        {
            // Set default chart areas
            Chart1.Series["Series2"].ChartArea = "Default";
            Chart1.Series["Series3"].ChartArea = "Default";

            // Remove newly created series and chart areas
            while(Chart1.Series.Count > 3)
            {
                Chart1.Series.RemoveAt(3);
            }
            while(Chart1.ChartAreas.Count > 1)
            {
                Chart1.ChartAreas.RemoveAt(1);
            }

            // Set default chart are position to Auto
            Chart1.ChartAreas["Default"].Position.Auto = true;
            Chart1.ChartAreas["Default"].InnerPlotPosition.Auto = true;

        }
    }

public void CreateYAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
    {
        // Create new chart area for original series
        ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
        areaSeries.BackColor = Color.Transparent;
        areaSeries.BorderColor = Color.Transparent;
        areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
        areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
        areaSeries.AxisX.MajorGrid.Enabled = false;
        areaSeries.AxisX.MajorTickMark.Enabled = false;
        areaSeries.AxisX.LabelStyle.Enabled = false;
        areaSeries.AxisY.MajorGrid.Enabled = false;
        areaSeries.AxisY.MajorTickMark.Enabled = false;
        areaSeries.AxisY.LabelStyle.Enabled = false;
        areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;


        series.ChartArea = areaSeries.Name;

        // Create new chart area for axis
        ChartArea areaAxis = chart.ChartAreas.Add("AxisY_" + series.ChartArea);
        areaAxis.BackColor = Color.Transparent;
        areaAxis.BorderColor = Color.Transparent;
        areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
        areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());

        // Create a copy of specified series
        Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
        seriesCopy.ChartType = series.ChartType;
        foreach(DataPoint point in series.Points)
        {
            seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
        }

        // Hide copied series
        seriesCopy.IsVisibleInLegend = false;
        seriesCopy.Color = Color.Transparent;
        seriesCopy.BorderColor = Color.Transparent;
        seriesCopy.ChartArea = areaAxis.Name;

        // Disable drid lines & tickmarks
        areaAxis.AxisX.LineWidth = 0;
        areaAxis.AxisX.MajorGrid.Enabled = false;
        areaAxis.AxisX.MajorTickMark.Enabled = false;
        areaAxis.AxisX.LabelStyle.Enabled = false;
        areaAxis.AxisY.MajorGrid.Enabled = false;
        areaAxis.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
        areaAxis.AxisY.LabelStyle.Font = area.AxisY.LabelStyle.Font;

        // Adjust area position
        areaAxis.Position.X -= axisOffset;
        areaAxis.InnerPlotPosition.X += labelsSize;

    }

结果:


Result:

这篇关于C#Winforms-创建具有多个Y轴(3个或更多)的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆