Click on a Chart Control in ASP: NET

1

I have defined a Chart that is called chart11 and I give it data in the following way using the load of the control

protected void Chart11_Load(object sender, EventArgs e)
{
    List<byte[]> Datos11 = new List<byte[]>();
    List<int> graficoY = new List<int>();
    Datos11 = selectAlarmas1(id);
    foreach (byte[] Datos in Datos11)
    {
        for (int i = 0; i < Datos.Length; i++)
        {
            graficoY.Add(Datos[i]);
        }
    }
    Console.Write("\n");

    foreach (int i in graficoY)
    {
        Console.Write("Y" + i + " ");
    }
    Console.Write("\n");

    for (int i = 0; i < graficoY.Count; i++)
    {
        Chart11.Series["Senales"].Points.AddY(graficoY[i]);
    }

    Chart11.Series["Senales"].PostBackValue = GetPostBackEventReference( Chart11_Click);
    Chart11.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = false;
    Chart11.ChartAreas["ChartArea1"].AxisY.LabelStyle.Enabled = false;
}

I want to click on that Chart but the event does not trigger:

protected void Chart11_Click(object sender, ImageMapEventArgs e)
{
    Grafica_Chart(10);
}

I would like to know how to make the event work.

    
asked by Sebastian Mateus Villegas 19.10.2016 в 17:29
source

1 answer

1

You need to verify that you have the property autopostback="true" and manipulate the properties in the series XValueMember YValueMember assigning the value you need to pass and PostBackValue which is the value of these required. Let's say:

<asp:Series Name="Miserie" XValueMember="TuValorODato" PostBackValue="#VALX" >

Clearer I put the following example:

First you must add the onClick event to your chart:

<asp:Chart ID="Chart1" runat="server" Height="287px" OnClick="Chart1_Click" OnLoad="Chart1_Load" Width="857px">
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

Then in your code behind

    protected void Chart1_Click(object sender, ImageMapEventArgs e)
    {
        Label1.Text ="El valor es:"+e.PostBackValue.ToString();
    }

    protected void Chart1_Load(object sender, EventArgs e)
    {
        int contador = 1;
        Chart1.Series.Add("Senales");
        while (contador < 10)
        {
            DataPoint punto = new DataPoint(contador,contador);
            Chart1.Series["Senales"].Points.Add(
            contador++;
        }
        Chart1.Series["Senales"].PostBackValue = "#VALX";
    }

The event is linked to the control not to the series as you have it in your code, in the field PostBackValue the item you want to receive must go by making the postback the value x of the column (XVAL) or the value y (YVAL) . Copy this example in your code so that you see it better is functional.

Additionally, I would suggest that you take a look at CanvasJS but that you need to learn basic JSON and jQuery javascript concepts but it is a more powerful tool than Microsoft VS and .NET default offer

    
answered by 19.10.2016 в 17:55