C#MSChart - 如何创建两个并排共享同一轴的饼图?

时间:2016-06-20 17:42:39

标签: c# charts mschart

我试图创建两个共享相同轴的饼图。有没有人有使用MSChart完成此任务的代码示例?

我想要实现的目标: Example of what I want to achieve

到目前为止,这是我的代码:

        Chart chart = new Chart();
        chart.Width = 800;
        string PreContractSeriesName = "PreContract";
        string ContractSeriesName = "Contract";

        //two chart areas
        chart.ChartAreas.Add(PreContractSeriesName);
        chart.ChartAreas.Add(ContractSeriesName);

        //two pie charts, so two series
        chart.Series.Add(PreContractSeriesName);
        chart.Series.Add(ContractSeriesName);

        //add series to chart area
        chart.Series[PreContractSeriesName].ChartArea = PreContractSeriesName;
        chart.Series[ContractSeriesName].ChartArea = ContractSeriesName;

        //set as pie chart
        chart.Series[PreContractSeriesName].ChartType = SeriesChartType.Pie;
        chart.Series[ContractSeriesName].ChartType = SeriesChartType.Pie;

        chart.ChartAreas[PreContractSeriesName].AlignmentOrientation = AreaAlignmentOrientations.Horizontal;
        chart.ChartAreas[ContractSeriesName].AlignmentOrientation = AreaAlignmentOrientations.Horizontal;

        chart.Series[PreContractSeriesName].Points.AddXY("source1", 100);
        chart.Series[PreContractSeriesName].Points.AddXY("source2", 200);
        chart.Series[PreContractSeriesName].Points.AddXY("source3", 300);
        chart.Series[PreContractSeriesName].Points.AddXY("source4", 400);
        chart.Series[PreContractSeriesName].Points.AddXY("source5", 500);

        chart.Series[ContractSeriesName].Points.AddXY("source1", 140);
        chart.Series[ContractSeriesName].Points.AddXY("source2", 240);
        chart.Series[ContractSeriesName].Points.AddXY("source3", 340);
        chart.Series[ContractSeriesName].Points.AddXY("source4", 440);
        chart.Series[ContractSeriesName].Points.AddXY("source5", 540);

        using (MemoryStream memStream = new MemoryStream())
        {
            chart.SaveImage(memStream, ChartImageFormat.Jpeg);

            return File(memStream.ToArray(), "image/jpeg");
        }

我的代码会产生以下图表:

enter image description here

我想知道:

  1. 如何并排排列图表?
  2. 这是分享轴的正确方法吗?
  3. 有没有人有一个如何实现这个目标的例子?
  4. 编辑:

    感谢用户TaW

    ,这是我的更新代码
            Chart chart = new Chart();
            chart.Width = 800;
            string PreContractSeriesName = "PreContract";
            string ContractSeriesName = "Contract";
    
            //two chart areas
            chart.ChartAreas.Add(PreContractSeriesName);
            chart.ChartAreas.Add(ContractSeriesName);
    
            //two pie charts, so two series
            chart.Series.Add(PreContractSeriesName);
            chart.Series.Add(ContractSeriesName);
    
            //add series to chart area
            chart.Series[PreContractSeriesName].ChartArea = PreContractSeriesName;
            chart.Series[ContractSeriesName].ChartArea = ContractSeriesName;
    
            //position chart areas side by side
            chart.ChartAreas[PreContractSeriesName].Position = new ElementPosition(2, 5, 48, 80);
            chart.ChartAreas[ContractSeriesName].Position = new ElementPosition(50, 5, 48, 80);
    
    
            chart.Legends.Add(new Legend() {
                Alignment = StringAlignment.Center,
                Docking = Docking.Bottom }
            );
            chart.Series[ContractSeriesName].IsVisibleInLegend = false;
    
            //set as pie chart
            chart.Series[PreContractSeriesName].ChartType = SeriesChartType.Pie;
            chart.Series[ContractSeriesName].ChartType = SeriesChartType.Pie;
    
            chart.Series[PreContractSeriesName].Points.AddXY("source1", 100);
            chart.Series[PreContractSeriesName].Points.AddXY("source2", 200);
            chart.Series[PreContractSeriesName].Points.AddXY("source3", 300);
            chart.Series[PreContractSeriesName].Points.AddXY("source4", 400);
            chart.Series[PreContractSeriesName].Points.AddXY("source5", 500);
    
            chart.Series[ContractSeriesName].Points.AddXY("source1", 140);
            chart.Series[ContractSeriesName].Points.AddXY("source2", 240);
            chart.Series[ContractSeriesName].Points.AddXY("source3", 340);
            chart.Series[ContractSeriesName].Points.AddXY("source4", 440);
            chart.Series[ContractSeriesName].Points.AddXY("source5", 540);
    

1 个答案:

答案 0 :(得分:2)

enter image description here

以下是如何在ChartAreas图表中对齐两个Chart ca1和ca2:

ca1.Position = new ElementPosition(2, 5, 48, 80);
ca2.Position = new ElementPosition(50, 5, 48, 80);
chart.Legends[0].Docking = Docking.Bottom;
chart.Legends[0].Alignment = StringAlignment.Center;

请注意,这会将位置硬编码为图表大小的百分比。

您尝试的对齐属性实际上是关于对齐数据而不是定位图表区域。看他们的AreaAlignmentStyles!要控制ChartArea的展示位置,我们需要保留默认Auto设置并选择我们自己的数字。

我向左和向右留下2%,让两个ChartAreas直接坐在一起。我添加了颜色,你可以看到它是什么..

我在Legend的底线上留下了15%,它停靠在底部并居中。

您需要使用自己的号码。

另请注意,默认情况下,两个馅饼各自获得自己的图例条目。要压制一组条目,您可以将此属性设置为Series S2

S2.IsVisibleInLegend = false;

另一个例子see here!