I am implementing chart 开发者_如何学编程controls .... I have three forms say form1 form2 form3 .....
in form1 I am showing some data in the form of pie chart
form2 i am showing some data in the form of bar chart .. thats working fine ...
my problem is i want to show these two charts in form 3 like over view form... where the user will see all charts ...
So when i click on the button the form 3 will be open with the two charts side by side (one is pie chart in form1 , another one is bar chart in form2) if you click on the one of the chart in form3 it will goes to the corresponding form..like this....
would any one have any idea about this how to implement ...
Many Thanks in advance....
I would do something this:
1) Create 2 user controls containing a mschart, and call them e.g. PieChartControl
and BarChartControl
. Expose a a method to set the current datasource (e.g. SetDataSource(DataTable dt)
) and put there the logic to bind the datasource to the PieChart or the BarChart
2) Create the 3 forms: in Form1
add PieChartControl
, in Form2
add BarChartControl
and in Form3
add a SplitContainer
where you will add both a PieChartControl
and a BarChartControl
.
3) Expose SetDataSource()
method also in Form1
and Form2
(it will just call the corresponding inner control method)
4) Expose also SetDataSource()
method in Form3
; it will call both SetDataSource()
methods of inner PieChartControl
and BarChartControl
.
5) Form3
has to expose also a custom property (e.g. ChartClicked
) indicating the chart that has been clicked
6) In Form3 subscribe the Click
event (or DoubleClick
, as you wish) for PieChartControl
and BarChartControl
7) When Click
event is triggered, just set the ChartClicked property and close the form
It will follow some code samples to help you understand my explanation.
Helper enum:
public enum ChartClicked { None = 0, Pie = 1, Bar = 2 }
MainForm:
// this is the form that have the button to open Form3
public partial class MainForm: Form
{
// other methods ...
private void openForm3ButtonClick(object sender,Eventargs e)
{
Form3 f3 = new Form3();
f3.SetDataSource(this.dataSrc)
f3.ShowDialog();
if(f3.ChartClicked == ChartClicked .Pie)
{
Form1 f1 = new Form1();
f1.SetDataSource(this.dataSrc);
f1.ShowDialog();
}
else if(f3.ChartClicked == ChartClicked .Bar)
{
Form2 f2 = new Form2();
f2.SetDataSource(this.dataSrc);
f2.ShowDialog();
}
}
}
Form3:
// the form having the 2 controls
public partial class Form3: Form
{
// other methods ...
public ChartClicked ChartClicked { get; private set; }
public Form3()
{
this.InitializeComponents();
this.PieChartControl.Click += chartControlClicked;
this.BarChartControl.Click += chartControlClicked;
}
public void SetDataSource(object src)
{
this.PieChartControl.SetDataSource(src);
this.BarChartControl.SetDataSource(src);
}
private void chartControlClicked(object sender, EventArgs e)
{
if(sender == this.PieChartControl)
this.ChartClicked = ChartClicked .Pie;
else if(sender == this.BarChartControl)
this.ChartClicked = ChartClicked .Bar;
this.Close();
}
}
Form1:
// the form having the pie chart control
public partial class Form1: Form
{
// other methods ...
public void SetDataSource(object src)
{
this.PieChartControl.SetDataSource(src);
}
}
Form2:
// the form having the bar chart control
public partial class Form2: Form
{
// other methods ...
public void SetDataSource(object src)
{
this.BarChartControl.SetDataSource(src);
}
}
PieChartControl:
public partial PieChartControl: UserControl
{
// other methods ...
public void SetDataSource(object src)
{
// set the series type to Pie etc...
this.chart.DataSource = src;
}
}
I can think of a couple of ways to do it.
1) Have your charts connect to a data source for their data. Chart in form 1 and chart 1 in form 3 connect to data source 1. Chart in form 2 and chart 2 in form 3 connect to the other data source. The charts will show the same data, so their appearance will be the same. This would be like many of the standard controls. However, if the user can interact with your charts you would need to block that interaction (have a read only property) for the charts in form 3 and duplicate all the events from the charts in forms 1 and 2 to the charts in form 3.
2) Add a method to your forms 1 and 2 (or charts 1 and 2) to get their current display as an Image. Form 3 contains 2 PictureBox controls which show those images. If there is any interaction you can periodically get the image on a timer event. Your chart control will probably have a cached copy of the image anyway that can be retrieved quickly.
精彩评论