在同一个图表上绘制两个不同的图表
我正在使用 MSChart Control 绘制一些数据,但我想绘制两个不同的图表 一起在同一张图表上。例如,使用条形图(系列1)和饼图(系列2)
我试图通过使用以下代码使用一个图表来显示两个不同的图表 它显示的数据正确,但一个问题是我想显示两个图表(饼图、条形图),但它显示(“两个饼图”)
任何人都可以提供帮助吗
注意:我对所有这些使用一张图表运营
private void kpicartmouse_click(object sender, MouseEventArgs e)
{
Series acceptseries;
Series membershiptypeseries = null;
Title title;
string area;
try
{
var pos = e.Location;
var results = kpiChartControl.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.DataPoint)
{
DataTable visits = null;
visits = KPIData.Visits(dtStartDate.Value,dtEndDate.Value, mf);
DataTable accepts = null;
accepts = KPIData.AcceptedvisitsByMembership(mf);
area = "subchart";
kpiChartControl.ChartAreas.Add(area);
acceptseries = kpiChartControl.Series.Add(area);
acceptseries.ChartArea = area;
title = kpiChartControl.Titles.Add("Accepted Visits By MemberShip Type");
title.DockedToChartArea = area;
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
kpiChartControl.Titles.Add("").DockedToChartArea = area;
kpiChartControl.Titles.Add("Accepted visits by memebrship types").DockedToChartArea = area;
foreach (Title titles in kpiChartControl.Titles)
{
titles.IsDockedInsideChartArea = false;
}
foreach (ChartArea chartArea in kpiChartControl.ChartAreas)
{
chartArea.Area3DStyle.Enable3D = true;
chartArea.Area3DStyle.Inclination = 45;
//chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview;
}
foreach (Series chartSeries in kpiChartControl.Series)
{
chartSeries.ChartType = SeriesChartType.StackedColumn;
chartSeries["ColumnDrawingStyle"] = "SoftEdge";
chartSeries["LabelStyle"] = "Top";
chartSeries.IsValueShownAsLabel = true;
chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
}
foreach (Series chartSeries in kpiChartControl.Series)
{
chartSeries.ChartType = SeriesChartType.Pie;
chartSeries["PieLabelStyle"] = "Outside";
chartSeries["DoughnutRadius"] = "30";
chartSeries["PieDrawingStyle"] = "SoftEdge";
chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
}
foreach (Legend legend in kpiChartControl.Legends)
{
legend.Enabled = false;
}
if (accepts == null)
{
acceptseries.Points.Clear();
acceptseries.Points.AddXY("no live", 0);
}
if (visits == null)
{
membershiptypeseries.Points.Clear();
membershiptypeseries.Points.AddXY("no membershiptypes", 0);
}
kpiChartControl.Series[0].Points.DataBindXY(accepts.Rows, "mshipname", accepts.Rows, "Value");
kpiChartControl.Series["subchart"].Points.DataBindXY(visits.Rows, "Status", visits.Rows, "Visits");
foreach (Series chartSeries in kpiChartControl.Series)
{
foreach (DataPoint point in chartSeries.Points)
{
switch (point.AxisLabel)
{
case "Accepted": point.Color = Color.Green; break;
case "Refused": point.Color = Color.Red; break;
}
point.Label = string.Format("{0:0}", point.YValues[0]);
}
}
foreach (Series chartSeries in kpiChartControl.Series)
{
foreach (DataPoint point in chartSeries.Points)
{
switch (point.AxisLabel)
{
case "Silver membership": point.Color = Color.Green; break;
case "Gold Membership": point.Color = Color.Blue; break;
//case "Refused": point.Color = Color.Red; break;
case "Weekend Peak": point.Color = Color.Cyan; break;
case "prspect": point.Color = Color.Indigo; break;
}
point.Label = string.Format("{0:0}- {1}", point.YValues[0], point.AxisLabel);
}
}
}
}
}
catch
{
}
}
I am using the MSChart Control to plot some data but I would like to plot two different graphs
together on the same chart. For example, use a Bar chart (series1) with a pie chart (series2)
i am trying to show the two different graphs using one single chart by using following code
It was showing data correct but one problem is i want to show the two graphs (PIE CHART , BAR CHART ) but it was showing ("TWO PIE Charts")
Can any one help on this
NOte : I am using one chart for all these operations
private void kpicartmouse_click(object sender, MouseEventArgs e)
{
Series acceptseries;
Series membershiptypeseries = null;
Title title;
string area;
try
{
var pos = e.Location;
var results = kpiChartControl.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.DataPoint)
{
DataTable visits = null;
visits = KPIData.Visits(dtStartDate.Value,dtEndDate.Value, mf);
DataTable accepts = null;
accepts = KPIData.AcceptedvisitsByMembership(mf);
area = "subchart";
kpiChartControl.ChartAreas.Add(area);
acceptseries = kpiChartControl.Series.Add(area);
acceptseries.ChartArea = area;
title = kpiChartControl.Titles.Add("Accepted Visits By MemberShip Type");
title.DockedToChartArea = area;
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
kpiChartControl.Titles.Add("").DockedToChartArea = area;
kpiChartControl.Titles.Add("Accepted visits by memebrship types").DockedToChartArea = area;
foreach (Title titles in kpiChartControl.Titles)
{
titles.IsDockedInsideChartArea = false;
}
foreach (ChartArea chartArea in kpiChartControl.ChartAreas)
{
chartArea.Area3DStyle.Enable3D = true;
chartArea.Area3DStyle.Inclination = 45;
//chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview;
}
foreach (Series chartSeries in kpiChartControl.Series)
{
chartSeries.ChartType = SeriesChartType.StackedColumn;
chartSeries["ColumnDrawingStyle"] = "SoftEdge";
chartSeries["LabelStyle"] = "Top";
chartSeries.IsValueShownAsLabel = true;
chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
}
foreach (Series chartSeries in kpiChartControl.Series)
{
chartSeries.ChartType = SeriesChartType.Pie;
chartSeries["PieLabelStyle"] = "Outside";
chartSeries["DoughnutRadius"] = "30";
chartSeries["PieDrawingStyle"] = "SoftEdge";
chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
}
foreach (Legend legend in kpiChartControl.Legends)
{
legend.Enabled = false;
}
if (accepts == null)
{
acceptseries.Points.Clear();
acceptseries.Points.AddXY("no live", 0);
}
if (visits == null)
{
membershiptypeseries.Points.Clear();
membershiptypeseries.Points.AddXY("no membershiptypes", 0);
}
kpiChartControl.Series[0].Points.DataBindXY(accepts.Rows, "mshipname", accepts.Rows, "Value");
kpiChartControl.Series["subchart"].Points.DataBindXY(visits.Rows, "Status", visits.Rows, "Visits");
foreach (Series chartSeries in kpiChartControl.Series)
{
foreach (DataPoint point in chartSeries.Points)
{
switch (point.AxisLabel)
{
case "Accepted": point.Color = Color.Green; break;
case "Refused": point.Color = Color.Red; break;
}
point.Label = string.Format("{0:0}", point.YValues[0]);
}
}
foreach (Series chartSeries in kpiChartControl.Series)
{
foreach (DataPoint point in chartSeries.Points)
{
switch (point.AxisLabel)
{
case "Silver membership": point.Color = Color.Green; break;
case "Gold Membership": point.Color = Color.Blue; break;
//case "Refused": point.Color = Color.Red; break;
case "Weekend Peak": point.Color = Color.Cyan; break;
case "prspect": point.Color = Color.Indigo; break;
}
point.Label = string.Format("{0:0}- {1}", point.YValues[0], point.AxisLabel);
}
}
}
}
}
catch
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是猜测......这段代码不会强制您所有的图表都是饼图吗?
如果这没有帮助,那么尝试将代码分解为更小的方法,然后依次注释每一位 - 然后您将能够理解每一位的作用。
Just a guess... won't this block of code force all your charts to be Pie charts?
If this doesn't help, then try breaking your code down into smaller methods, then commenting each bit out in turn - then you'll be able to understand what each bit does.