ExpertPDF Components: .NET Chart Control

WebChart Chart Click Event

Learn how you can handle the chart click event

Chart Events

ExpertChart control offers the possibility to handle the ChartClick event.

Chart Type:
Chart Click Data:

100 100 200 200 110 170 300 140 290 100 60 100 300 160 120 200 120 190 200 160 220 220 80 120 270 200 100 300 230 190 260 100 200 320 190 270 For more information visit www.chartingcontrol.net

Source code

    private System.Data.DataTable dt;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        dt = ExpertChart.Demo.DataSources.GetTestData();
 
        if (!IsPostBack)
        {
            LoadChartType();
            WebChart1.DataSource = dt;
        }
    }
 
    protected void DdlChartType_SelectedIndexChanged(object sender, EventArgs e)
    {
        WebChart1.ChartType = (ExpertChart.ChartType)Enum.Parse(typeof(ExpertChart.ChartType), DdlChartType.SelectedValue);
        LitData.Text = string.Empty;
    }
 
    private void LoadChartType()
    {
        DdlChartType.Items.Clear();
 
        string[] types = Enum.GetNames(typeof(ExpertChart.ChartType));
        for (int i = 0; i <= types.Length - 1; i++)
        {
            DdlChartType.Items.Add(types[i]);
        }
 
    }
 
    protected void  WebChart1_ChartClick(object sender, ExpertChart.ChartClickEventArgs e)
    {
        LitData.Text = 
            "Record index: " + e.RecordIndex + "<br>" + 
            "Column index: " + e.ColumnIndex + "<br>" + 
            "X Axis label: " + dt.Rows[e.RecordIndex]["Labels"].ToString() + "<br>" +
            "Y value clicked: " + dt.Rows[e.RecordIndex][e.ColumnIndex + 2].ToString(); 
        // e.ColumnIndex + 2 because ColumnIndex is the 0-based index of the columns displayed in the chart as data series. The data source has 2 more extra data columns.
    }
 
 
    /// <summary>
    /// This method returns a sample DataTable with test data
    /// The tables has the following structure:
    /// - Labels (string)
    /// - Date (DateTime)
    /// - Seller1 (number)
    /// - Seller2 (number)
    /// - Seller3 (number)
    /// </summary>
    public static DataTable GetTestData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Labels", typeof(string));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Seller1", typeof(int));
        dt.Columns.Add("Seller2", typeof(int));
        dt.Columns.Add("Seller3", typeof(int));
 
        DataRow row = dt.NewRow();
        row["Date"] = new DateTime(2006, 1, 1);
        row["Labels"] = "January";
        row["Seller1"] = 100;
        row["Seller2"] = 100;
        row["Seller3"] = 200;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 2, 1);
        row["Labels"] = "February";
        row["Seller1"] = 200;
        row["Seller2"] = 110;
        row["Seller3"] = 170;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 3, 1);
        row["Labels"] = "March";
        row["Seller1"] = 300;
        row["Seller2"] = 140;
        row["Seller3"] = 290;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 4, 1);
        row["Labels"] = "April";
        row["Seller1"] = 100;
        row["Seller2"] = 60;
        row["Seller3"] = 100;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 5, 1);
        row["Labels"] = "May";
        row["Seller1"] = 300;
        row["Seller2"] = 160;
        row["Seller3"] = 120;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 6, 1);
        row["Labels"] = "June";
        row["Seller1"] = 200;
        row["Seller2"] = 120;
        row["Seller3"] = 190;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 7, 1);
        row["Labels"] = "June";
        row["Seller1"] = 200;
        row["Seller2"] = 160;
        row["Seller3"] = 220;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 8, 1);
        row["Labels"] = "August";
        row["Seller1"] = 220;
        row["Seller2"] = 80;
        row["Seller3"] = 120;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 9, 1);
        row["Labels"] = "September";
        row["Seller1"] = 270;
        row["Seller2"] = 200;
        row["Seller3"] = 100;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 10, 1);
        row["Labels"] = "October";
        row["Seller1"] = 300;
        row["Seller2"] = 230;
        row["Seller3"] = 190;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 11, 1);
        row["Labels"] = "November";
        row["Seller1"] = 260;
        row["Seller2"] = 100;
        row["Seller3"] = 200;
        dt.Rows.Add(row);
 
        row = dt.NewRow();
        row["Date"] = new DateTime(2006, 12, 1);
        row["Labels"] = "December";
        row["Seller1"] = 320;
        row["Seller2"] = 190;
        row["Seller3"] = 270;
        dt.Rows.Add(row);
 
        return dt;
 
    }

Copyright 2008. ExpertComponents.com ChartingControl.NET.