problem with mschart in winforms
I am big problem for drawing two different charts on the same form...
I have one form, in that form i have represented the pie chart with data coming from database that was working fine.....
and i have managed that when i click on the specific part on the pie chart i want to show two different graphs .. upto click event i have managaed but I am not able to show two different charts ....
and this is my below code...
public void mschart1_MouseClick(object sender, MouseEventArgs e)
{
// here i am checking the condition to hit the exact location in chart
Series serries;
Series series2 = null;
DataTable new1 = null;
new1 = mData.accountstatus();
DataTable new2 = null;
new2 = mdata2.Totals(dtpStartDate.Value, dtpenddate.Value);
msch开发者_如何学Cart1.ChartAreas.Clear();
mschart1.Titles.Clear();
mschart1.Series.Clear();
area = "area1";
mschart1.ChartAreas.Add(area);
serries = mschart1.Series.Add(area);
serries.ChartArea = area;
title = mschart1.Titles.Add("title1");
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
title.DockedToChartArea = area;
mschart1.Titles.Add("").DockedToChartArea = area;
area = "area2";
mschart1.ChartAreas.Add(area);
series2 = mschart1.Series.Add(area);
series2.ChartArea = area;
title = mschart1.Titles.Add("title2");
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
title.Alignment = ContentAlignment.TopLeft;
title.DockedToChartArea = area;
mschart1.Titles.Add("").DockedToChartArea = area;
foreach (Title chartTitle in mschart1.Titles)
{
chartTitle.IsDockedInsideChartArea = false;
}
foreach (ChartArea chartArea in mschart1.ChartAreas)
{
chartArea.Area3DStyle.Enable3D = true;
chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;
}
if (area == "area2")
{
foreach (Series charttypes in mschart1.Series)
{
charttypes.ChartType = SeriesChartType.Pie;
charttypes["PielabelStyle"] = "Outside";
charttypes["DoughnutRadius"] = "30";
charttypes["PieDrawingStyle"] = "SoftEdge";
charttypes.BackGradientStyle = GradientStyle.DiagonalLeft;
}
}
if (area == "area1")
{
foreach (Series chartareas in mschart1.Series)
{
chartareas.ChartType = SeriesChartType.StackedColumn;
chartareas["ColumnDrawingStyle"] = "SoftEdge";
chartareas["LabelStyle"] = "Top";
chartareas.IsValueShownAsLabel = true;
chartareas.BackGradientStyle = GradientStyle.DiagonalLeft;
}
}
foreach (Legend legend in mschart1.Legends)
{
legend.Enabled = false;
}
if (new2 == null)
{
series2.Points.Clear();
series2.Points.AddXY("no data", 0);
}
if(new1 == null)
{
serries.Points.Clear();
serries.Points.AddXY("no data", 0);
}
mschart1.Series["area1"].Points.DataBindXY(new2.Rows, "data1", new2.Rows, "Value");
mschart1.Series["area2"].Points.DataBindXY(new1.Rows, "name", new2.Rows, "count");
if (area == "area1")
{
foreach (Series charttypes in mschart1.Series)
{
foreach (DataPoint point in charttypes.Points)
{
switch (point.AxisLabel)
{
case "case1": point.Color = Color.Cyan; break;
case "case2": point.Color = Color.Green; break;
}
point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
}
}
}
if (area == "area2")
{
foreach (Series chartareas in mschart1.Series)
{
foreach (DataPoint point in chartareas.Points)
{
switch (point.AxisLabel)
{
case "case23": point.Color = Color.Green; break;
case "case 24": point.Color = Color.Blue; break;
}
point.Label = string.Format("{0:0}", point.YValues[0]);
}
}
}
}
my problem when i click on the chart in specific area it will shows two charts but both are same charts ..(i want two different type of charts as i have mentioned in the below code)
would any one pls help on this......
Many thanks...
I guess the problem is in the pieces (two) of code like this one:
if (area == "area2")
{
foreach (Series charttypes in mschart1.Series)
{
charttypes.ChartType = SeriesChartType.Pie;
...
}
}
if (area == "area1")
{
foreach (Series charttypes in mschart1.Series)
{
charttypes.ChartType = SeriesChartType.StackedColumn;
...
}
}
Since area
variable is lastly set to "area2"
, only the first if
condition will be reached and so the chart type will be always of type Pie.
I think you should change your code to something like:
foreach (Series charttypes in mschart1.Series)
{
if(charttypes.ChartArea == "area2")
{
charttypes.ChartType = SeriesChartType.Pie;
...
}
else if(charttypes.ChartArea == "area1")
{
charttypes.ChartType = SeriesChartType.StackedColumn;
...
}
}
精彩评论