|
Pie chart postback |
Posted by TL on Jan-06-2016 19:16 |
|
Hello
I would like to see a code example shows how to cause a postback (instead of redirecting
to another page) when a slice of a pie chart is clicked.
So far I tried the code below but the GetPostBackURL() method throws "Object reference
not set to an instance of an object" exception.
private void initPieChart()
{
WebChartViewer wcvWebChartViewer = new WebChartViewer();
int nChartWidth = 300;
PieChart pcChart = new PieChart(nChartWidth, 220);
//set data to pie chart...
//design pie chart...
//deal with click events:
wcvWebChartViewer.Image = pcChart.makeWebImage(Chart.PNG);
//make chart clickable
wcvWebChartViewer.ImageMap = pcChart.getHTMLImageMap(
wcvWebChartViewer.GetPostBackURL() + "?checkResult=
{label}&strStatus=up",
"", "title='bla bla bla {label}'");
wcvWebChartViewer.ClickHotSpot += new
WebHotSpotEventHandler(pieChartSlice_Click);
}
public void pieChartSlice_Click(object sender, WebHotSpotEventArgs e)
{
} |
Re: Pie chart postback |
Posted by Peter Kwan on Jan-07-2016 01:08 |
|
Hi TL,
As PostBack is a feature of the WebForm, to use PostBack, the WebChartViewer must be
on a WebForm. In your code, it seems the WebChartViewer is not on a WebForm when you
call GetPostBackURL. (Because some of your code is omitted, so I am not absolutely sure.)
Have you insert the WebChartViewer into the WebForm first, before calling
GetPostBackURL?
Regards
Peter Kwan |
Re: Pie chart postback |
Posted by TL on Jan-07-2016 04:34 |
|
Thank you for the reply,
I had a command that added the WebChartViewer to a place holder that I have on the
page. It was after the line where the image map is created. I located it before the image
map creation and now it's working.
However, the method that handles the ClickHotSpot event is not invoked when I click on a
slice (but Page load does). What can I do?
private void initPieChart()
{
WebChartViewer wcvWebChartViewer = new WebChartViewer();
int nChartWidth = 300;
PieChart pcChart = new PieChart(nChartWidth, 220);
//set data to pie chart...
//design pie chart...
chCheckResultUp.Controls.Add(wcvWebChartViewer);
wcvWebChartViewer.Image = pcChart.makeWebImage(Chart.PNG);
wcvWebChartViewer.ImageMap = pcChart.getHTMLImageMap(
wcvWebChartViewer.GetPostBackURL(),
"", "title='bla bla {label}'");
wcvWebChartViewer.ClickHotSpot += new
WebHotSpotEventHandler(PieSlice_Click);
}
public void PieSlice_Click(object sender, WebHotSpotEventArgs e)
{
} |
Re: Pie chart postback |
Posted by Peter Kwan on Jan-08-2016 00:43 |
|
Hi TL,
In your case, the WebChartViewer is dynamically created. Have you created the
WebChartViewer before the event occur? You would need to create it in Page_Init or
Page_Load. If you do not created it, the object does not exist and the event cannot be
invoked.
Below is an example that I have just tried and it works in my case. Please try it to see
if it works in your case. If the example works, but your real code does not work, is it
possible to create an example that does not work (perhaps by modify my code) to help
me reproduce the problem?
Regards
Peter Kwan
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="ChartDirector" %>
<%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir"
%>
<!DOCTYPE html>
<script runat="server">
//
// Page Load event handler
//
protected void Page_Load(object sender, EventArgs e)
{
WebChartViewer v = new WebChartViewer();
Panel1.Controls.Add(v);
v.ClickHotSpot += new WebHotSpotEventHandler(WebChartViewer1_ClickHotSpot);
// The data for the pie chart
double[] data = {25, 18, 15, 12, 8, 30, 35};
// The labels for the pie chart
string[] labels = {"Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities",
"Production"};
// Create a PieChart object of size 360 x 300 pixels
PieChart c = new PieChart(360, 300);
// Set the center of the pie at (180, 140) and the radius to 100 pixels
c.setPieSize(180, 140, 100);
// Set the pie data and the pie labels
c.setData(data, labels);
// Output the chart
v.Image = c.makeWebImage(Chart.PNG);
// Include tool tip for the chart
v.ImageMap = c.getHTMLImageMap(v.GetPostBackURL(), "", "title='{label}:
US${value}K ({percent}%)'"
);
}
protected void WebChartViewer1_ClickHotSpot(object sender, WebHotSpotEventArgs e)
{
System.Diagnostics.Debug.WriteLine("ppp");
}
</script>
<html>
<head>
<title>Simple Pie Chart</title>
</head>
<body style="margin:5px 0px 0px 5px">
<form runat="server">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html> |
Re: Pie chart postback |
Posted by TL on Jan-10-2016 16:55 |
|
Thank You so much!
Indeed, I created the WebChartViewer inside the "if (!Page.IsPostBack)" clause. Now that i
relocated the initiation of the pie chart outside this clause, the event handler is hit.
Thank you! |
|