您好我试图使用mschart控件在一个表单上表示两个图表(饼图和条形图)....数据来自数据库......
我使用单图(kpichart)控件来表示两个图....
因为我在代码
之下在这段代码中,我定义了两个不同的区域来定义两个图形.....
public void KpiMembers_MouseClick(object sender, MouseEventArgs e)
{
Series statusseries;
Series liveserries = null;
string area;
Title title;
try
{
var pos = e.Location;
var results = KpiMembersTotalchart.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.DataPoint)
{
if (result.Series.Points[result.PointIndex].AxisLabel == "Live")
{
DataTable Livemembers = null;
Livemembers = KpiData.livemembersmembershipstatus();
DataTable membersalltotals = null;
membersalltotals = kpimembtotals.membershipTotals(dtpStartDate.Value, dtpenddate.Value);
KpiMembersTotalchart.ChartAreas.Clear();
KpiMembersTotalchart.Titles.Clear();
KpiMembersTotalchart.Series.Clear();
area = "newchart";
KpiMembersTotalchart.ChartAreas.Add(area);
liveserries = KpiMembersTotalchart.Series.Add(area);
liveserries.ChartArea = area;
title = KpiMembersTotalchart.Titles.Add("Memberstotalspoints");
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
title.DockedToChartArea = area;
KpiMembersTotalchart.Titles.Add("").DockedToChartArea = area;
area = "subchart";
KpiMembersTotalchart.ChartAreas.Add(area);
statusseries = KpiMembersTotalchart.Series.Add(area);
statusseries.ChartArea = area;
title = KpiMembersTotalchart.Titles.Add("Live Status members by status");
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
title.DockedToChartArea = area;
KpiMembersTotalchart.Titles.Add("").DockedToChartArea = area;
foreach (Title chartTitle in KpiMembersTotalchart.Titles)
{
chartTitle.IsDockedInsideChartArea = false;
}
foreach (ChartArea chartArea in KpiMembersTotalchart.ChartAreas)
{
chartArea.Area3DStyle.Enable3D = true;
chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;
}
if (area == "subchart")
{
foreach (Series charttypes in KpiMembersTotalchart.Series)
{
charttypes.ChartType = SeriesChartType.Pie;
charttypes["PielabelStyle"] = "Outside";
charttypes["DoughnutRadius"] = "30";
charttypes["PieDrawingStyle"] = "SoftEdge";
charttypes.BackGradientStyle = GradientStyle.DiagonalLeft;
}
}
if (area == "newchart")
{
foreach (Series chartareas in KpiMembersTotalchart.Series)
{
chartareas.ChartType = SeriesChartType.StackedColumn;
chartareas["ColumnDrawingStyle"] = "SoftEdge";
chartareas["LabelStyle"] = "Top";
chartareas.IsValueShownAsLabel = true;
chartareas.BackGradientStyle = GradientStyle.DiagonalLeft;
}
}
foreach (Legend legend in KpiMembersTotalchart.Legends)
{
legend.Enabled = false;
}
if (membersalltotals == null)
{
statusseries.Points.Clear();
statusseries.Points.AddXY("no status", 0);
}
if(Livemembers == null)
{
liveserries.Points.Clear();
liveserries.Points.AddXY("no membershipe", 0);
}
KpiMembersTotalchart.Series["newchart"].Points.DataBindXY(membersalltotals.Rows, "Status", membersalltotals.Rows, "Value");
KpiMembersTotalchart.Series["subchart"].Points.DataBindXY(Livemembers.Rows, "mshipname", Livemembers.Rows, "count");
if (area == "subchart")
{
foreach (Series charttypes in KpiMembersTotalchart.Series)
{
foreach (DataPoint point in charttypes.Points)
{
switch (point.AxisLabel)
{
case "New": point.Color = Color.Cyan; break;
case "Live": point.Color = Color.Green; break;
case "Defaulter": point.Color = Color.Red; break;
case "Cancelled": point.Color = Color.Orange; break;
case "Completed": point.Color = Color.Blue; break;
case "Frozen": point.Color = Color.Violet; break;
}
point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
}
}
}
if (area == "newchart")
{
foreach (Series chartareas in KpiMembersTotalchart.Series)
{
foreach (DataPoint point in chartareas.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}", point.YValues[0]);
}
}
}
}
}
}
}
catch { }
}
我的问题是我试图显示两个不同的图表(一个是饼图,另一个是堆积柱形图)....
但是它显示了两个相同的图表(都是饼图),请参见下图[{0}}
我想在第二个中显示堆积的柱形图而不是饼图....但是它显示了两个相同的图表(饼图)
我将总表格划分为两个区域.....
我需要做些什么才能使用单一图表控件在同一表格上显示两个不同的图表
任何人都会帮忙解决这个问题....
非常感谢任何想法......
答案 0 :(得分:0)
这是代码中逻辑的一个简单问题。
您的代码目前为:
area = "newchart";
......
area = "subchart";
......
if (area == "subchart")
......
if (area == "newchart")
这显然会始终将第一个“if”评估为“true”,将第二个“if”评估为“false”。您需要重新组织代码,以便它不会像这样使用area
- 您需要退后一步并重写您创建所需图表的逻辑。