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

Message ListMessage List     Post MessagePost Message

  Javascript not working
Posted by adib on Aug-14-2016 09:46
Hi, i'm working on a website using c# as the codebehind. I'm trying to embed chart director onto my website.  I've tried several ways but it seems the javascript won't work.

One of it is that I put the chart image in codebehind, and the javascript in aspx.

-------------------------------
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="LandingPage.aspx.cs" Inherits="GSO.LandingPage" %>
<%@ Import Namespace="ChartDirector" %>
<%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir" %>

<asp:Content ID="Head" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>

<asp:Content ID="Chart" runat="server" ContentPlaceHolderID="ChartContent">

   <script type="text/javascript">

        //
        // Use the window load event to set up the MouseMovePlotArea event handler
        //
        JsChartViewer.addEventListener(window, 'load', function () {
            var viewer = JsChartViewer.get('<%=WCviewer.ClientID%>');

            // Draw track cursor when mouse is moving over plotarea
            viewer.attachHandler(["MouseMovePlotArea", "TouchStartPlotArea", "TouchMovePlotArea", "ChartMove", "Now"],
            function (e) {
                this.preventDefault(e);   // Prevent the browser from using touch events for other actions
                trackLineLegend(viewer, viewer.getPlotAreaMouseX());
            });
        });

        //
        // Draw track line with legend
        //
        function trackLineLegend(viewer, mouseX) {
            // Remove all previously drawn tracking object
            viewer.hideObj("all");

            // The chart and its plot area
            var c = viewer.getChart();
            var plotArea = c.getPlotArea();

            // Get the data x-value that is nearest to the mouse, and find its pixel coordinate.
            var xValue = c.getNearestXValue(mouseX);
            var xCoor = c.getXCoor(xValue);
            if (xCoor == null)
                return;

            // Draw a vertical track line at the x-position
            viewer.drawVLine("trackLine", xCoor, plotArea.getTopY(), plotArea.getBottomY(), "black 1px dotted");

            // Array to hold the legend entries
            var legendEntries = [];

            // Iterate through all layers to build the legend array
            for (var i = 0; i < c.getLayerCount() ; ++i) {
                var layer = c.getLayerByZ(i);

                // The data array index of the x-value
                var xIndex = layer.getXIndexOf(xValue);

                // Iterate through all the data sets in the layer
                for (var j = 0; j < layer.getDataSetCount() ; ++j) {
                    var dataSet = layer.getDataSetByZ(j);

                    // We are only interested in visible data sets with names, as they are required for legend entries.
                    var dataName = dataSet.getDataName();
                    var color = dataSet.getDataColor();
                    if ((!dataName) || (color == null))
                        continue;

                    // Build the legend entry, consist of a colored square box, the name and the data value.
                    var dataValue = dataSet.getValue(xIndex);
                    legendEntries.push("<nobr>" + viewer.htmlRect(7, 7, color) + " " + dataName + ": " +
                        ((dataValue == null) ? "N/A" : dataValue.toPrecision(4)) + viewer.htmlRect(20, 0) + "</nobr> ");

                    // Draw a track dot for data points within the plot area
                    var yCoor = c.getYCoor(dataSet.getPosition(xIndex), dataSet.getUseYAxis());
                    if ((yCoor != null) && (yCoor >= plotArea.getTopY()) && (yCoor <= plotArea.getBottomY())) {
                        viewer.showTextBox("dataPoint" + i + "_" + j, xCoor, yCoor, JsChartViewer.Center,
                            viewer.htmlRect(7, 7, color));
                    }
                }
            }

            // Create the legend by joining the legend entries.
            var legend = "<nobr>[" + c.xAxis().getFormattedLabel(xValue, "mm/dd/yyyy") + "]" + viewer.htmlRect(20, 0) +
                "</nobr> " + legendEntries.reverse().join("");

            // Display the legend on the top of the plot area
            viewer.showTextBox("legend", plotArea.getLeftX(), plotArea.getTopY(), JsChartViewer.BottomLeft, legend,
                "width:" + plotArea.getWidth() + "px;font:bold 11px Arial;padding:3px;-webkit-text-size-adjust:100%;");
        }

</script>

</asp:Content>

<asp:Content ID="Body" runat="server" ContentPlaceHolderID="MainContent">

</asp:Content>

<asp:Content ID="Foot" runat="server" ContentPlaceHolderID="FootContent">

</asp:Content>

----------------------------------------

What I did was simply copied the sample, particularly tracklegend.aspx, into both of these files. The chart works alright, appearing as expected, but the tracking feature just doesnt work. I've also tried putting everything in aspx, under the Chart ContentHolder as the code above, and also injecting the javascript into my codebehind using RegisterStartupScript, to no avail.

Can anyone help me on this?

  Re: Javascript not working
Posted by Peter Kwan on Aug-16-2016 02:44
Attachments:
Hi adib,

Without seeing the complete code or the exact output of your code, it is hard to diagnose the problem. Some common issue is that the cdjcv.js is not included or the path is not correct, the charting code has not included the getJsChartModel line, or the "window onload" event handler is not executed at all.

The "window onload" event handler (the code in the "addEventListener" line) should be executed when the web page is completely loaded. If your page is not loaded as a web page (eg. it is loaded as part of a web page using Javascript), it may not trigger the window onload event and the code would not be executed as all. In this case, the Javascript that load the web page would need to call a function to execute the event handler directly.

I have attached an example for your reference that uses a master page, and put the charting code in code behind for your reference.

If you need further help, is it possible to provide a simple example (similar to the files attached) or to provide the "output" of your code? If you are using IE to view the web page, you can select "File/Save As" and save the complete Web Page.

Hope this can help.

Regards
Peter Kwan
Site.Master
Site.Master

383 bytes
Default.aspx
Default.aspx

4.27 Kb
   
Default.aspx.cs
using System;
using System.Web.UI;
using ChartDirector;


namespace WebApplication5
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Data for the chart as 3 random data series
            RanSeries r = new RanSeries(127);
            double[] data0 = r.getSeries(100, 100, -15, 15);
            double[] data1 = r.getSeries(100, 150, -15, 15);
            double[] data2 = r.getSeries(100, 200, -15, 15);
            DateTime[] timeStamps = r.getDateSeries(100, new DateTime(2011, 1, 1), 86400);

            // Create a XYChart object of size 640 x 400 pixels
            XYChart c = new XYChart(640, 400);

            // Add a title to the chart using 18pt 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, 0xf0f6ff, 0xa0c0ff), -1, Chart.Transparent, 0xffffff, 0xffffff);


            // Set axis label style to 8pt 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 10pt 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.
            LineLayer layer = c.addLineLayer2();
            layer.setLineWidth(2);

            // Add 3 data series to the line layer
            layer.setXData(timeStamps);
            layer.addDataSet(data0, 0xff3333, "Alpha");
            layer.addDataSet(data1, 0x008800, "Beta");
            layer.addDataSet(data2, 0x3333cc, "Gamma");

            // Output the chart
            WCviewer.Image = c.makeWebImage(Chart.PNG);

            // Output Javascript chart model to the browser to suppport tracking cursor
            WCviewer.ChartModel = c.getJsChartModel();
        }
    }
}
Default.aspx.designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace WebApplication5 {
    
    
    public partial class _Default {
        
        /// <summary>
        /// form1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm form1;
        
        /// <summary>
        /// WCviewer control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ChartDirector.WebChartViewer WCviewer;
    }
}

  Re: Javascript not working
Posted by adib on Aug-17-2016 00:40
Ah thanks so much peter.

Everything was in place, except that I registered cdjcv.js in the page instead of the site master. Alright thanks again