gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:11:22 25 4
gpt4 key购买 nike

我正在尝试并排创建两个共享同一轴的饼图。有没有人有使用 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. 有没有人举例说明如何完成此操作?

编辑:

感谢用户 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);

最佳答案

enter image description here

下面是如何在 Chart 图表中对齐两个 ChartAreas 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!

关于C# MSChart - 如何并排创建共享同一轴的两个饼图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37928674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com