我这样做是为了使用mschart控件为图表的一部分创建点击事件处理程序,图表就像这样
和像这样的代码
private void targetChartmouse_Click(object sender, MouseEventArgs e)
{
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)
{
//do something....
}
}
}
}
当我们点击图表(在图表的每个部分)时它工作正常,但我想只在我们点击实时(绿色)而不是每个部分时做某事。是否有可能找到活的区域(绿色)
是否可以使用c#?
我正在做winforms应用程序
修改后的代码
public void targetChartmouse_Click(object sender, MouseEventArgs e)
{
Series statusseries = new Series();
Series liveseries = null;
Title title;
string area;
//Series totalserries;
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 accepts = null;
accepts = KPIData.livemembersmembershiptype(mf);
DataTable membershiptypes = null;
membershiptypes = KPIData.MembershipTotals(dtStartDate.Value, dtEndDate.Value, mf);
area = "subchart";
kpiChartControl.ChartAreas.Add(area);
statusseries = kpiChartControl.Series.Add(area);
statusseries.ChartArea = area;
title = kpiChartControl.Titles.Add("Live Status members 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("Live status membership types").DockedToChartArea = area;
area = "";
kpiChartControl.Titles.Add("").DockedToChartArea = area;
foreach (Title titles in kpiChartControl.Titles)
{
titles.IsDockedInsideChartArea = false;
}
foreach (ChartArea chartArea in kpiChartControl.ChartAreas)
{
chartArea.Area3DStyle.Enable3D = true;
chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;
//chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview;
}
if (area == "subchart")
{
foreach (Series chartSeries in kpiChartControl.Series)
{
chartSeries.ChartType = SeriesChartType.StackedColumn;
chartSeries["ColumnDrawingStyle"] = "SoftEdge";
chartSeries["LabelStyle"] = "Top";
chartSeries.IsValueShownAsLabel = true;
chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
}
}
else if (area == "subchart")
{
foreach (Series chartSeries in kpiChartControl.Series)
{
chartSeries.ChartType = SeriesChartType.Pie;
//chartSeries["PieLabelStyle"] = "Outside";
chartSeries["PieLabelStyle"] = "Inside";
chartSeries["DoughnutRadius"] = "30";
chartSeries["PieDrawingStyle"] = "SoftEdge";
chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
}
}
foreach (Legend legend in kpiChartControl.Legends)
{
legend.Enabled = false;
}
if (membershiptypes == null)
{
statusseries.Points.Clear();
statusseries.Points.AddXY("no status", 0);
}
if (accepts == null)
{
liveseries.Points.Clear();
liveseries.Points.AddXY("no live", 0);
}
kpiChartControl.Series["subchart"].Points.DataBindXY(accepts.Rows, "mshipname", accepts.Rows, "count");
kpiChartControl.Series[0].Points.DataBindXY(membershiptypes.Rows, "Status", membershiptypes.Rows, "Value");
}
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}", point.YValues[0]);
}
}
foreach (Series chartSeries in kpiChartControl.Series)
{
foreach (DataPoint point in chartSeries.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);
}
}
}
}
catch
{
}
}
答案 0 :(得分:1)
这可能有效:
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)
{
if (result.Series.Points[result.PointIndex].AxisLabel == "Live")
{
Console.WriteLine("success?");
}
}
}
}
答案 1 :(得分:0)
也许您可以使用[DatePoint
获取result.PointIndex
(http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.hittestresult.pointindex .aspx)和result.Series
。
if(result.Series.Points.Item[result.PointIndex].Label == "Live") {
// ...
}