ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Problem with ChartModel in Updatepanel .Net version
Posted by Roy B on Jan-13-2013 01:58
Attachments:
I am having difficulty with updating a chart inside an updatepanel.  The chart updates OK
but the ChartModel does not.  The attached example derived from the tracklabel example
demonstates the problem.

If you run the example and click the button after initial page load then the graph will display
and update on each click, but the track line does not appear.

If you remove the <trigger> from the updatepanel, then the trackline works, but a whole
page refresh is occurring which is what I want to avoid as the real application has many
more elements on the page.
WebForm1.aspx
WebForm1.aspx

4.91 Kb
    
WebForm1.aspx.vb
Imports ChartDirector
Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub
    Protected Sub buildGraph()
        Dim r As RanSeries = New RanSeries(Now().Second)
        Dim data0() As Double = r.getSeries(100, 100, -15, 15)
        Dim data1() As Double = r.getSeries(100, 150, -15, 15)
        Dim data2() As Double = r.getSeries(100, 200, -15, 15)
        Dim timeStamps() As Date = r.getDateSeries(100, DateSerial(2011, 1, 1), 86400)

        ' Create a XYChart object of size 640 x 400 pixels
        Dim c As XYChart = New XYChart(640, 400)

        ' Add a title to the chart using 18 pts Times New Roman Bold Italic font
        c.addTitle("    Product Line Global Revenue", "Times New Roman Bold Italic", 18)

        ' Set the plotarea at (50, 55) with width 70 pixels less than chart width, and height 90 pixels
        ' less than chart height. Use a vertical gradient from light blue (f0f6ff) to sky blue (a0c0ff)
        ' as background. Set border to transparent and grid lines to white (ffffff).
        c.setPlotArea(50, 55, c.getWidth() - 70, c.getHeight() - 90, c.linearGradientColor(0, 55, 0, _
            c.getHeight() - 35, &HF0F6FF, &HA0C0FF), -1, Chart.Transparent, &HFFFFFF, &HFFFFFF)

        ' Add a legend box at (50, 25) using horizontal layout. Use 10pts Arial Bold as font. Set the
        ' background and border color to Transparent.
        c.addLegend(50, 25, False, "Arial Bold", 10).setBackground(Chart.Transparent)

        ' Set axis label style to 8pts Arial Bold
        c.xAxis().setLabelStyle("Arial Bold", 8)
        c.yAxis().setLabelStyle("Arial Bold", 8)

        ' Set the axis stem to transparent
        c.xAxis().setColors(Chart.Transparent)
        c.yAxis().setColors(Chart.Transparent)

        ' Configure x-axis label format
        c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", _
            Chart.StartOfMonthFilter(), "{value|mm}")

        ' Add axis title using 10pts Arial Bold Italic font
        c.yAxis().setTitle("USD millions", "Arial Bold Italic", 10)

        ' Add a line layer to the chart using a line width of 2 pixels.
        Dim layer As LineLayer = c.addLineLayer2()
        layer.setLineWidth(2)

        ' Add 3 data series to the line layer
        layer.setXData(timeStamps)
        layer.addDataSet(data0, &HFF3333, "Alpha")
        layer.addDataSet(data1, &H8800, "Beta")
        layer.addDataSet(data2, &H3333CC, "Gamma")

        ' Output the chart
        WebChartViewer1.Image = c.makeWebImage(Chart.PNG)

        ' Output Javascript chart model to the browser to suppport tracking cursor
        WebChartViewer1.ChartModel = c.getJsChartModel()

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        buildGraph()
    End Sub
End Class

  Re: Problem with ChartModel in Updatepanel .Net version
Posted by Peter Kwan on Jan-17-2013 06:34
Hi Roy B,

Sorry for the late reply.

We found out the reason that the "track cursor" does not work are as follows:

(a) In the code, the MouseMovePlotArea event handler is set up when the web page is completely loaded (the addEventListener is configured to listen to the Javascript window load event). However, in your case, the chart does not yet exist. The chart is created subsequently using the update panel by ajax means. Furthermore, the update panel works by deleting all previous contents in it, and insert new contents. So any event handlers that is set up will be deleted. To solve the problem, the event handler needs to be set up everytime new contents are loaded to the update panel.

(b) The update panel does not execute any Javascript in its contents. So the Javascript generated by WebChartViewer is ignored by the update panel. To solve this problem, we need additional code to ensure the Javascript is executed.

The steps to solve the above problem is as follows:

Instead of using:

JsChartViewer.addEventListener(window, 'load', function() {
   ....
}

please change the code to:

function UpdatePanel1_PostUpdate() {

     // Make sure Javascript code is run
     var content = document.getElementById("UpdatePanel1").innerHTML;
     var script = /<script [^>]*>(.*?)<\\/script>/igm;
     var code;
     while (code = script.exec(content))
          eval(code[1]);

      // Now can set up the event handlers
      var viewer = JsChartViewer.get('<%=WebChartViewer1.ClientID%>');

      // Draw track cursor when mouse is moving over plotarea. Hide it when mouse leaves plot area.
      viewer.attachHandler("MouseMovePlotArea", function(e) {
           trackLineLabel(viewer, viewer.getPlotAreaMouseX());
           viewer.setAutoHide("all", "MouseOutPlotArea");
      });
}

Then in the page load event handler, use the following code to make sure the UpdatePanel1_PostUpdate is executed everytime the UpdatePanel1 is updated:

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "UpdatePanel1_PostUpdate", "UpdatePanel1_PostUpdate();", True)

Hope this can help.

Regards
Peter Kwan

  Re: Problem with ChartModel in Updatepanel .Net version
Posted by Roy B on Jan-17-2013 23:04
Many thanks.  This works perfectly in my main application page, just had to modify

// Make sure Javascript code is run
            var content =
document.getElementById("UpdatePanel1").innerHTML;

to

// Make sure Javascript code is run
            var content =
document.getElementById('<%=UpdatePanel1.ClientID%>').innerHTML;

as the updatepanel was embedded in other content.

  Re: Problem with ChartModel in Updatepanel .Net version
Posted by Roy B on Feb-13-2013 06:46
Unfortunately I am still experiencing an issue with this.  The code works fine after the first
AJAX update. On the second and any subsequent update however the trackline does not
disappear when the mouse moves out of the plot area.  This is not too much of an issue in
the simple example, but on my main application the trackline stays visible when the user
selects another tab in the tab panel.

  Re: Problem with ChartModel in Updatepanel .Net version
Posted by Peter Kwan on Feb-14-2013 03:06
Hi Roy,

We apologize very much for this issue. After detail testing, we believe it may be caused by a bug in the ChartDirector Javascript library cdjcv.js.

The bug is that if the chart image <IMG> is destroyed and then recreated, the JsChartViewer may lose track of which HTML objects are pre-existing, and which HTML objects are created by itself. As a result, hideObj("all") may not work correctly.

I have just uploaded an updated cdjcv.js to our web site which should solve the problem:

http://www.advsofteng.com/cdjcv51p2.zip

Please kindly let me know if it does solve the problem.

Regards
Peter Kwan

  Re: Problem with ChartModel in Updatepanel .Net version
Posted by Roy B on Feb-22-2013 23:59
Thank you Peter.  This has fixed the problem.