我想创建两个并排的饼图,共享同一轴。有没有使用MSChart实现这个目标的代码示例?以下是我想要实现的示例:
以下是我目前的代码:
![Example of what I want to achieve](https://istack.dev59.com/fhKrW.webp)
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");
}
我的代码会产生以下重叠的图表:
我想知道:- 如何将图表并排对齐?
- 这是共享轴的正确方式吗?
- 有没有人有实现这个的例子?
感谢用户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);