ASP.net chart refreshing page - asp.net

I realized that the asp:chart controler was refreshing the page.
For example if I click on a button to display a chart, the current page is refreshed, but I don't want it to be.
Let's say I want to diplay two charts. I have the following code in my default.aspx file (code generated with the toolbox):
<div>
<asp:Chart ID="Chart1" runat="server">
<series>
<asp:Series Name="Series1">
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</chartareas>
</asp:Chart>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
<div>
<asp:Chart ID="Chart2" runat="server">
<series>
<asp:Series Name="Series1">
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</chartareas>
</asp:Chart>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
</div>
And the behindcode in the default.aspx.cs file:
protected void Button1_Click(object sender, EventArgs e)
{
double[] yValues = { 71.15, 23.19, 5.66 };
string[] xValues = { "AAA", "BBB", "CCC" };
Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);
}
protected void Button2_Click(object sender, EventArgs e)
{
double[] yValues = { 71.15, 23.19, 5.66 };
string[] xValues = { "DDD", "EEE", "FFF" };
Chart2.Series["Series1"].Points.DataBindXY(xValues, yValues);
}
My problem is that when I click on the Button1, my first chart appears. But then when I click on the Button2, the first chart disappear and the second one appears.
I would like to keep both charts displayed!
Is it possible? How?

You have 2 buttons on the page, each one firing its own click event. This way, of course you will see just one chart a time.
If you want to see both all the time, do it like this (both buttons pointing to a shared event handler):
<div>
<asp:Chart ID="Chart1" runat="server">
<series>
<asp:Series Name="Series1">
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</chartareas>
</asp:Chart>
<asp:Button ID="Button1" runat="server" onclick="ChartButtons_Click" Text="Button" />
</div>
<div>
<asp:Chart ID="Chart2" runat="server">
<series>
<asp:Series Name="Series1">
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</chartareas>
</asp:Chart>
<asp:Button ID="Button2" runat="server" onclick="ChartButtons_Click" Text="Button" />
</div>
On your CS file:
protected void ChartButtons_Click(object sender, EventArgs e)
{
double[] yValues = { 71.15, 23.19, 5.66 };
string[] xValues = { "AAA", "BBB", "CCC" };
Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);
yValues = { 71.15, 23.19, 5.66 };
xValues = { "DDD", "EEE", "FFF" };
Chart2.Series["Series1"].Points.DataBindXY(xValues, yValues);
}

Related

How to set Chart control width into percentage

In asp.net chart control, how do we set chart width into percentage, below scenario is chart width needs to be set with the panel width. As the chart takes only pixel. Help on this.
<table style="width:100%;">
<tr>
<td style="width:50%;border:1px solid black;">
<asp:Panel ID="pnl" runat="server" Width="100%">
<asp:Chart ID="Chart2" runat="server" >
<Titles><asp:Title Text="ChartSample1"></asp:Title></Titles>
<Series>
<asp:Series Name="Series1"></asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
</asp:Chart>
</asp:Panel>
</td>
<td style="width:50%;border:1px solid black;">
<asp:Panel ID="pnl2" runat="server" Width="100%">
<asp:Chart ID="Chart3" runat="server">
<Titles><asp:Title Text="ChartSample2"></asp:Title></Titles>
<Series>
<asp:Series Name="Series1"></asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
</asp:Chart>
</asp:Panel>
</td>
</tr>
</table>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
Chart2.Series[0].Points.AddXY(1, 1);
Chart2.Series[0].Points.AddXY(1, 2);
Chart2.Series[0].Points.AddXY(2, 1);
Chart2.Series[0].Points.AddXY(2, 2);
Chart2.Series[0].Points.AddXY(3, 1);
Chart2.Series[0].Points.AddXY(3, 2);
Chart3.Series[0].Points.AddXY(1, 10);
Chart3.Series[0].Points.AddXY(1, 20);
Chart3.Series[0].Points.AddXY(2, 10);
Chart3.Series[0].Points.AddXY(2, 20);
Chart3.Series[0].Points.AddXY(3, 10);
Chart3.Series[0].Points.AddXY(3, 20);
}
If you have not found a solution, you may try getting the width and height properties of the panel in codebehind.
I preferred using panels in the website instead of table.
double heightTable = panel1.height.value;
chart2.height = new unit(heighttable);

How to show values on ASP.NET Chart

Here is my chart:
ASP.NET code:
<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" Width="752px">
<Series>
<asp:Series Name="Series1" XValueMember="Column2" YValueMembers="Column1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderColor="DarkGray">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
How can I show values on blue boxes?
You can do this from your codebehind:
Chart1.Series["Series1"].IsValueShownAsLabel = true;
You can also do this from your ASPX code:
<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" Width="752px">
<Series>
<asp:Series Name="Series1" XValueMember="Column2" YValueMembers="Column1" IsValueShownAsLabel="true">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderColor="DarkGray">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>

How do you hide the white border in a Asp.net Chart

How do I hide the white border that appears round the chart using the ASP.NET charting control?
<asp:Chart ID="chtGender" runat="server" BorderSkin-BorderColor="Green" Width="300" Height="350">
<Series>
<asp:Series ChartType="Pie" Palette="EarthTones" ChartArea="MainChartArea">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="MainChartArea" BackColor ="Black" BorderColor = "Black" BorderWidth= "0" Area3DStyle-Enable3D="true">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
I had the same problem and couldnt find a proper solution.
The way I did it was set the borderwidth on the "chartarea" to 20 which overwrote the white border around my pie chart. eg:-
<asp:Chart id="myPieChart" runat="server">
<series>
<asp:Series Name="Series1" ChartType="Pie">
<Points>
<asp:DataPoint YValues="45" />
<asp:DataPoint YValues="34" />
<asp:DataPoint YValues="67" />
</Points>
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="ChartArea1"
BackColor="#E6F1FA"
BorderDashStyle="Solid"
BorderWidth="20"
BorderColor="#E6F1FA">
</asp:ChartArea>
</chartareas>
</asp:Chart>

ASP.NET markup has two controls, A, then B. Why does B appear first?

Here's my full markup:
<%#page Title="" Language="VB" MasterPageFile="~/Master Pages/MasterPage.master" AutoEventWireup="false" CodeFile="Statistics.aspx.vb" Inherits="Statistics" %>
<%# Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="phPageContent" runat="Server">
<asp:Label ID="Label1" runat="server" Text="Label" CssClass="CaptionLabel"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="DefaultDropDown" />
<asp:Chart ID="Chart1" runat="server" Height="438px" Width="877px" Palette="Bright">
<Series>
<asp:Series ChartArea="ChartArea1" ChartType="Line" Legend="Legend1" Name="Clicks">
</asp:Series>
<asp:Series ChartArea="ChartArea1" ChartType="Line" Legend="Legend1" Name="Conversions">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<AxisY Title="Count" TitleFont="Microsoft Sans Serif, 10pt">
</AxisY>
<AxisX Title="Month" TitleFont="Microsoft Sans Serif, 10pt">
</AxisX>
<AxisY2 TextOrientation="Rotated270" Title="Commission (USD)" TitleFont="Microsoft Sans Serif, 12pt, style=Bold">
</AxisY2>
</asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Name="Legend1" Title="Legend">
<CellColumns>
<asp:LegendCellColumn Name="Column1" Text="#SERIESNAME">
<Margins Left="15" Right="15" />
</asp:LegendCellColumn>
<asp:LegendCellColumn ColumnType="SeriesSymbol" Name="Column2">
<Margins Left="15" Right="15" />
</asp:LegendCellColumn>
</CellColumns>
</asp:Legend>
</Legends>
<Titles>
<asp:Title Font="Microsoft Sans Serif, 12pt" Name="Title1" Text="Monthly Clicks and Conversions ">
</asp:Title>
</Titles>
</asp:Chart>
</asp:Content>
And here's how it renders. The code behind simply adds the points.
Why does the CHART appear AFTER the label and drop down list control if the label appear first in the flow?
The dropdownlist IS appearing after the chart. If the chart had a border or if you were to add a tag after the dropdown, this will become more obvious.

Can we show 3 Yvalues on chart control?

I have a web application on which there are 2 chart control as follows:
<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1"
Height="589px" Width="1109px">
<Series>
<asp:Series Name="Series1" IsValueShownAsLabel="True" LabelAngle="90"
XValueMember="Question" YValueMembers="YesCounter">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<AxisY Title="Number of Yes">
</AxisY>
<AxisX Title="Questions" IsLabelAutoFit="False">
<LabelStyle Angle="90" Interval="1" />
</AxisX>
<Position Height="94" Width="50" X="3" Y="3" />
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
and
<asp:Chart ID="Chart2" runat="server" DataSourceID="SqlDataSource2"
Height="589px" Width="1109px">
<Series>
<asp:Series Name="Series1" IsValueShownAsLabel="True" LabelAngle="90"
XValueMember="Question" YValueMembers="NoCounter">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<AxisY Title="Number of No">
</AxisY>
<AxisX Title="Questions" IsLabelAutoFit="False">
<LabelStyle Angle="90" Interval="1" />
</AxisX>
<Position Height="94" Width="50" X="3" Y="3" />
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
Now I want to show one single chart that contains questions on the X-axis and a "yes,no" count on the Y-axis. How this can be done?
You can use stacked bar chart. Something like this http://www.dotnetcharting.com/gallery/view.aspx?id=Gallery/a03

Resources