ChartDirector 6.2 (.NET Edition)

Arbitrary XY Line Chart




This example demonstrates drawing a line chart with arbitrary x coordinates (not increasing or decreasing in one direction), and adding custom data labels to data points.

The x values of the data points are set into the chart using Layer.setXData. ChartDirector merely joins the points together with lines. It does not require the points to following any particular direction.

Note that this example has special labels for the start and end points of the lines. They are created using Layer.addCustomDataLabel.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\xyline.cs
using System;
using ChartDirector;

namespace CSharpChartExplorer
{
    public class xyline : DemoModule
    {
        //Name of demo module
        public string getName() { return "Arbitrary XY Line Chart"; }

        //Number of charts produced in this demo module
        public int getNoOfCharts() { return 1; }

        //Main code for creating chart.
        //Note: the argument chartIndex is unused because this demo only has 1 chart.
        public void createChart(WinChartViewer viewer, int chartIndex)
        {
            // The (x, y) data for the first line
            double[] dataX0 = {20, 90, 40, 30, 12};
            double[] dataY0 = {10, 40, 75, 54, 20};

            // The (x, y) data for the second line
            double[] dataX1 = {10, 40, 75, 54, 60};
            double[] dataY1 = {50, 90, 40, 30, 10};

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

            // Set the plotarea at (55, 65) and of size 350 x 300 pixels, with white background and
            // a light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with
            // light grey color (0xc0c0c0)
            c.setPlotArea(55, 65, 350, 300, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1);

            // Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 12pt
            // Times Bold Italic font. Set the background and border color to Transparent.
            c.addLegend(50, 30, false, "Times New Roman Bold Italic", 12).setBackground(
                Chart.Transparent);

            // Add a title to the chart using 18pt Times Bold Itatic font
            c.addTitle("Reaction Path", "Times New Roman Bold Italic", 18);

            // Add a title to the y axis using 12pt Arial Bold Italic font
            c.yAxis().setTitle("Temperature (Celcius)", "Arial Bold Italic", 12);

            // Set the y axis line width to 3 pixels
            c.yAxis().setWidth(3);

            // Add a title to the x axis using 12pt Arial Bold Italic font
            c.xAxis().setTitle("Pressure (Pa)", "Arial Bold Italic", 12);

            // Set the x axis line width to 3 pixels
            c.xAxis().setWidth(3);

            // Add a red (0xff3333) line layer using dataX0 and dataY0
            LineLayer layer1 = c.addLineLayer(dataY0, 0xff3333, "Compound AAA");
            layer1.setXData(dataX0);

            // Set the line width to 3 pixels
            layer1.setLineWidth(3);

            // Use 9 pixel square symbols for the data points
            layer1.getDataSet(0).setDataSymbol(Chart.SquareSymbol, 9);

            // Add custom text labels to the first and last point on the scatter plot using Arial
            // Bold font
            layer1.addCustomDataLabel(0, 0, "Start", "Arial Bold");
            layer1.addCustomDataLabel(0, 4, "End", "Arial Bold");

            // Add a green (0x33ff33) line layer using dataX1 and dataY1
            LineLayer layer2 = c.addLineLayer(dataY1, 0x33ff33, "Compound BBB");
            layer2.setXData(dataX1);

            // Set the line width to 3 pixels
            layer2.setLineWidth(3);

            // Use 11 pixel diamond symbols for the data points
            layer2.getDataSet(0).setDataSymbol(Chart.DiamondSymbol, 11);

            // Add custom text labels to the first and last point on the scatter plot using Arial
            // Bold font
            layer2.addCustomDataLabel(0, 0, "Start", "Arial Bold");
            layer2.addCustomDataLabel(0, 4, "End", "Arial Bold");

            // Output the chart
            viewer.Chart = c;

            //include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("clickable", "",
                "title='[{dataSetName}] Pressure = {x} Pa, Temperature = {value} C'");
        }
    }
}

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\xyline.vb
Imports System
Imports Microsoft.VisualBasic
Imports ChartDirector

Public Class xyline
    Implements DemoModule

    'Name of demo module
    Public Function getName() As String Implements DemoModule.getName
        Return "Arbitrary XY Line Chart"
    End Function

    'Number of charts produced in this demo module
    Public Function getNoOfCharts() As Integer Implements DemoModule.getNoOfCharts
        Return 1
    End Function

    'Main code for creating chart.
    'Note: the argument chartIndex is unused because this demo only has 1 chart.
    Public Sub createChart(viewer As WinChartViewer, chartIndex As Integer) _
        Implements DemoModule.createChart

        ' The (x, y) data for the first line
        Dim dataX0() As Double = {20, 90, 40, 30, 12}
        Dim dataY0() As Double = {10, 40, 75, 54, 20}

        ' The (x, y) data for the second line
        Dim dataX1() As Double = {10, 40, 75, 54, 60}
        Dim dataY1() As Double = {50, 90, 40, 30, 10}

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

        ' Set the plotarea at (55, 65) and of size 350 x 300 pixels, with white background and a
        ' light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light
        ' grey color (0xc0c0c0)
        c.setPlotArea(55, 65, 350, 300, &Hffffff, -1, &Hc0c0c0, &Hc0c0c0, -1)

        ' Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 12pt Times
        ' Bold Italic font. Set the background and border color to Transparent.
        c.addLegend(50, 30, False, "Times New Roman Bold Italic", 12).setBackground( _
            Chart.Transparent)

        ' Add a title to the chart using 18pt Times Bold Itatic font
        c.addTitle("Reaction Path", "Times New Roman Bold Italic", 18)

        ' Add a title to the y axis using 12pt Arial Bold Italic font
        c.yAxis().setTitle("Temperature (Celcius)", "Arial Bold Italic", 12)

        ' Set the y axis line width to 3 pixels
        c.yAxis().setWidth(3)

        ' Add a title to the x axis using 12pt Arial Bold Italic font
        c.xAxis().setTitle("Pressure (Pa)", "Arial Bold Italic", 12)

        ' Set the x axis line width to 3 pixels
        c.xAxis().setWidth(3)

        ' Add a red (0xff3333) line layer using dataX0 and dataY0
        Dim layer1 As LineLayer = c.addLineLayer(dataY0, &Hff3333, "Compound AAA")
        layer1.setXData(dataX0)

        ' Set the line width to 3 pixels
        layer1.setLineWidth(3)

        ' Use 9 pixel square symbols for the data points
        layer1.getDataSet(0).setDataSymbol(Chart.SquareSymbol, 9)

        ' Add custom text labels to the first and last point on the scatter plot using Arial Bold
        ' font
        layer1.addCustomDataLabel(0, 0, "Start", "Arial Bold")
        layer1.addCustomDataLabel(0, 4, "End", "Arial Bold")

        ' Add a green (0x33ff33) line layer using dataX1 and dataY1
        Dim layer2 As LineLayer = c.addLineLayer(dataY1, &H33ff33, "Compound BBB")
        layer2.setXData(dataX1)

        ' Set the line width to 3 pixels
        layer2.setLineWidth(3)

        ' Use 11 pixel diamond symbols for the data points
        layer2.getDataSet(0).setDataSymbol(Chart.DiamondSymbol, 11)

        ' Add custom text labels to the first and last point on the scatter plot using Arial Bold
        ' font
        layer2.addCustomDataLabel(0, 0, "Start", "Arial Bold")
        layer2.addCustomDataLabel(0, 4, "End", "Arial Bold")

        ' Output the chart
        viewer.Chart = c

        'include tool tip for the chart
        viewer.ImageMap = c.getHTMLImageMap("clickable", "", _
            "title='[{dataSetName}] Pressure = {x} Pa, Temperature = {value} C'")

    End Sub

End Class

[WPF - C#] NetWPFCharts\xyline.cs
using System;
using ChartDirector;

namespace CSharpWPFDemo
{
    public class xyline : DemoModule
    {
        //Name of demo module
        public string getName() { return "Arbitrary XY Line Chart"; }

        //Number of charts produced in this demo module
        public int getNoOfCharts() { return 1; }

        //Main code for creating chart.
        //Note: the argument chartIndex is unused because this demo only has 1 chart.
        public void createChart(WPFChartViewer viewer, int chartIndex)
        {
            // The (x, y) data for the first line
            double[] dataX0 = {20, 90, 40, 30, 12};
            double[] dataY0 = {10, 40, 75, 54, 20};

            // The (x, y) data for the second line
            double[] dataX1 = {10, 40, 75, 54, 60};
            double[] dataY1 = {50, 90, 40, 30, 10};

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

            // Set the plotarea at (55, 65) and of size 350 x 300 pixels, with white background and
            // a light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with
            // light grey color (0xc0c0c0)
            c.setPlotArea(55, 65, 350, 300, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1);

            // Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 12pt
            // Times Bold Italic font. Set the background and border color to Transparent.
            c.addLegend(50, 30, false, "Times New Roman Bold Italic", 12).setBackground(
                Chart.Transparent);

            // Add a title to the chart using 18pt Times Bold Itatic font
            c.addTitle("Reaction Path", "Times New Roman Bold Italic", 18);

            // Add a title to the y axis using 12pt Arial Bold Italic font
            c.yAxis().setTitle("Temperature (Celcius)", "Arial Bold Italic", 12);

            // Set the y axis line width to 3 pixels
            c.yAxis().setWidth(3);

            // Add a title to the x axis using 12pt Arial Bold Italic font
            c.xAxis().setTitle("Pressure (Pa)", "Arial Bold Italic", 12);

            // Set the x axis line width to 3 pixels
            c.xAxis().setWidth(3);

            // Add a red (0xff3333) line layer using dataX0 and dataY0
            LineLayer layer1 = c.addLineLayer(dataY0, 0xff3333, "Compound AAA");
            layer1.setXData(dataX0);

            // Set the line width to 3 pixels
            layer1.setLineWidth(3);

            // Use 9 pixel square symbols for the data points
            layer1.getDataSet(0).setDataSymbol(Chart.SquareSymbol, 9);

            // Add custom text labels to the first and last point on the scatter plot using Arial
            // Bold font
            layer1.addCustomDataLabel(0, 0, "Start", "Arial Bold");
            layer1.addCustomDataLabel(0, 4, "End", "Arial Bold");

            // Add a green (0x33ff33) line layer using dataX1 and dataY1
            LineLayer layer2 = c.addLineLayer(dataY1, 0x33ff33, "Compound BBB");
            layer2.setXData(dataX1);

            // Set the line width to 3 pixels
            layer2.setLineWidth(3);

            // Use 11 pixel diamond symbols for the data points
            layer2.getDataSet(0).setDataSymbol(Chart.DiamondSymbol, 11);

            // Add custom text labels to the first and last point on the scatter plot using Arial
            // Bold font
            layer2.addCustomDataLabel(0, 0, "Start", "Arial Bold");
            layer2.addCustomDataLabel(0, 4, "End", "Arial Bold");

            // Output the chart
            viewer.Chart = c;

            //include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("clickable", "",
                "title='[{dataSetName}] Pressure = {x} Pa, Temperature = {value} C'");
        }
    }
}

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\xyline.aspx
(Click here on how to convert this code to code-behind style.)
<%@ 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)
{
    // The (x, y) data for the first line
    double[] dataX0 = {20, 90, 40, 30, 12};
    double[] dataY0 = {10, 40, 75, 54, 20};

    // The (x, y) data for the second line
    double[] dataX1 = {10, 40, 75, 54, 60};
    double[] dataY1 = {50, 90, 40, 30, 10};

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

    // Set the plotarea at (55, 65) and of size 350 x 300 pixels, with white background and a light
    // grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light grey color
    // (0xc0c0c0)
    c.setPlotArea(55, 65, 350, 300, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1);

    // Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 12pt Times Bold
    // Italic font. Set the background and border color to Transparent.
    c.addLegend(50, 30, false, "Times New Roman Bold Italic", 12).setBackground(Chart.Transparent);

    // Add a title to the chart using 18pt Times Bold Itatic font
    c.addTitle("Reaction Path", "Times New Roman Bold Italic", 18);

    // Add a title to the y axis using 12pt Arial Bold Italic font
    c.yAxis().setTitle("Temperature (Celcius)", "Arial Bold Italic", 12);

    // Set the y axis line width to 3 pixels
    c.yAxis().setWidth(3);

    // Add a title to the x axis using 12pt Arial Bold Italic font
    c.xAxis().setTitle("Pressure (Pa)", "Arial Bold Italic", 12);

    // Set the x axis line width to 3 pixels
    c.xAxis().setWidth(3);

    // Add a red (0xff3333) line layer using dataX0 and dataY0
    LineLayer layer1 = c.addLineLayer(dataY0, 0xff3333, "Compound AAA");
    layer1.setXData(dataX0);

    // Set the line width to 3 pixels
    layer1.setLineWidth(3);

    // Use 9 pixel square symbols for the data points
    layer1.getDataSet(0).setDataSymbol(Chart.SquareSymbol, 9);

    // Add custom text labels to the first and last point on the scatter plot using Arial Bold font
    layer1.addCustomDataLabel(0, 0, "Start", "Arial Bold");
    layer1.addCustomDataLabel(0, 4, "End", "Arial Bold");

    // Add a green (0x33ff33) line layer using dataX1 and dataY1
    LineLayer layer2 = c.addLineLayer(dataY1, 0x33ff33, "Compound BBB");
    layer2.setXData(dataX1);

    // Set the line width to 3 pixels
    layer2.setLineWidth(3);

    // Use 11 pixel diamond symbols for the data points
    layer2.getDataSet(0).setDataSymbol(Chart.DiamondSymbol, 11);

    // Add custom text labels to the first and last point on the scatter plot using Arial Bold font
    layer2.addCustomDataLabel(0, 0, "Start", "Arial Bold");
    layer2.addCustomDataLabel(0, 4, "End", "Arial Bold");

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

    // Include tool tip for the chart
    WebChartViewer1.ImageMap = c.getHTMLImageMap("", "",
        "title='[{dataSetName}] Pressure = {x} Pa, Temperature = {value} C'");
}

</script>

<html>
<body>
    <chart:WebChartViewer id="WebChartViewer1" runat="server" />
</body>
</html>

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\xyline.aspx
(Click here on how to convert this code to code-behind style.)
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="ChartDirector" %>
<%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir" %>

<!DOCTYPE html>

<script runat="server">

'
' Page Load event handler
'
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

    ' The (x, y) data for the first line
    Dim dataX0() As Double = {20, 90, 40, 30, 12}
    Dim dataY0() As Double = {10, 40, 75, 54, 20}

    ' The (x, y) data for the second line
    Dim dataX1() As Double = {10, 40, 75, 54, 60}
    Dim dataY1() As Double = {50, 90, 40, 30, 10}

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

    ' Set the plotarea at (55, 65) and of size 350 x 300 pixels, with white background and a light
    ' grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light grey color
    ' (0xc0c0c0)
    c.setPlotArea(55, 65, 350, 300, &Hffffff, -1, &Hc0c0c0, &Hc0c0c0, -1)

    ' Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 12pt Times Bold
    ' Italic font. Set the background and border color to Transparent.
    c.addLegend(50, 30, False, "Times New Roman Bold Italic", 12).setBackground(Chart.Transparent)

    ' Add a title to the chart using 18pt Times Bold Itatic font
    c.addTitle("Reaction Path", "Times New Roman Bold Italic", 18)

    ' Add a title to the y axis using 12pt Arial Bold Italic font
    c.yAxis().setTitle("Temperature (Celcius)", "Arial Bold Italic", 12)

    ' Set the y axis line width to 3 pixels
    c.yAxis().setWidth(3)

    ' Add a title to the x axis using 12pt Arial Bold Italic font
    c.xAxis().setTitle("Pressure (Pa)", "Arial Bold Italic", 12)

    ' Set the x axis line width to 3 pixels
    c.xAxis().setWidth(3)

    ' Add a red (0xff3333) line layer using dataX0 and dataY0
    Dim layer1 As LineLayer = c.addLineLayer(dataY0, &Hff3333, "Compound AAA")
    layer1.setXData(dataX0)

    ' Set the line width to 3 pixels
    layer1.setLineWidth(3)

    ' Use 9 pixel square symbols for the data points
    layer1.getDataSet(0).setDataSymbol(Chart.SquareSymbol, 9)

    ' Add custom text labels to the first and last point on the scatter plot using Arial Bold font
    layer1.addCustomDataLabel(0, 0, "Start", "Arial Bold")
    layer1.addCustomDataLabel(0, 4, "End", "Arial Bold")

    ' Add a green (0x33ff33) line layer using dataX1 and dataY1
    Dim layer2 As LineLayer = c.addLineLayer(dataY1, &H33ff33, "Compound BBB")
    layer2.setXData(dataX1)

    ' Set the line width to 3 pixels
    layer2.setLineWidth(3)

    ' Use 11 pixel diamond symbols for the data points
    layer2.getDataSet(0).setDataSymbol(Chart.DiamondSymbol, 11)

    ' Add custom text labels to the first and last point on the scatter plot using Arial Bold font
    layer2.addCustomDataLabel(0, 0, "Start", "Arial Bold")
    layer2.addCustomDataLabel(0, 4, "End", "Arial Bold")

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

    ' Include tool tip for the chart
    WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", _
        "title='[{dataSetName}] Pressure = {x} Pa, Temperature = {value} C'")

End Sub

</script>

<html>
<body>
    <chart:WebChartViewer id="WebChartViewer1" runat="server" />
</body>
</html>

[ASP.NET MVC - Controller] NetMvcCharts\Controllers\XylineController.cs
using System;
using System.Web.Mvc;
using ChartDirector;

namespace NetMvcCharts.Controllers
{
    public class XylineController : Controller
    {
        //
        // Default Action
        //
        public ActionResult Index()
        {
            ViewBag.Title = "Arbitrary XY Line Chart";
            createChart(ViewBag.Viewer = new RazorChartViewer(HttpContext, "chart1"));
            return View("~/Views/Shared/ChartView.cshtml");
        }

        //
        // Create chart
        //
        private void createChart(RazorChartViewer viewer)
        {
            // The (x, y) data for the first line
            double[] dataX0 = {20, 90, 40, 30, 12};
            double[] dataY0 = {10, 40, 75, 54, 20};

            // The (x, y) data for the second line
            double[] dataX1 = {10, 40, 75, 54, 60};
            double[] dataY1 = {50, 90, 40, 30, 10};

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

            // Set the plotarea at (55, 65) and of size 350 x 300 pixels, with white background and a
            // light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light
            // grey color (0xc0c0c0)
            c.setPlotArea(55, 65, 350, 300, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1);

            // Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 12pt Times
            // Bold Italic font. Set the background and border color to Transparent.
            c.addLegend(50, 30, false, "Times New Roman Bold Italic", 12).setBackground(Chart.Transparent
                );

            // Add a title to the chart using 18pt Times Bold Itatic font
            c.addTitle("Reaction Path", "Times New Roman Bold Italic", 18);

            // Add a title to the y axis using 12pt Arial Bold Italic font
            c.yAxis().setTitle("Temperature (Celcius)", "Arial Bold Italic", 12);

            // Set the y axis line width to 3 pixels
            c.yAxis().setWidth(3);

            // Add a title to the x axis using 12pt Arial Bold Italic font
            c.xAxis().setTitle("Pressure (Pa)", "Arial Bold Italic", 12);

            // Set the x axis line width to 3 pixels
            c.xAxis().setWidth(3);

            // Add a red (0xff3333) line layer using dataX0 and dataY0
            LineLayer layer1 = c.addLineLayer(dataY0, 0xff3333, "Compound AAA");
            layer1.setXData(dataX0);

            // Set the line width to 3 pixels
            layer1.setLineWidth(3);

            // Use 9 pixel square symbols for the data points
            layer1.getDataSet(0).setDataSymbol(Chart.SquareSymbol, 9);

            // Add custom text labels to the first and last point on the scatter plot using Arial Bold
            // font
            layer1.addCustomDataLabel(0, 0, "Start", "Arial Bold");
            layer1.addCustomDataLabel(0, 4, "End", "Arial Bold");

            // Add a green (0x33ff33) line layer using dataX1 and dataY1
            LineLayer layer2 = c.addLineLayer(dataY1, 0x33ff33, "Compound BBB");
            layer2.setXData(dataX1);

            // Set the line width to 3 pixels
            layer2.setLineWidth(3);

            // Use 11 pixel diamond symbols for the data points
            layer2.getDataSet(0).setDataSymbol(Chart.DiamondSymbol, 11);

            // Add custom text labels to the first and last point on the scatter plot using Arial Bold
            // font
            layer2.addCustomDataLabel(0, 0, "Start", "Arial Bold");
            layer2.addCustomDataLabel(0, 4, "End", "Arial Bold");

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

            // Include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("", "",
                "title='[{dataSetName}] Pressure = {x} Pa, Temperature = {value} C'");
        }
    }
}

[ASP.NET MVC - View] NetMvcCharts\Views\Shared\ChartView.cshtml
@{ Layout = null; }

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body style="margin:5px 0px 0px 5px">
    <div style="font:bold 18pt verdana;">
        @ViewBag.Title
    </div>
    <hr style="border:solid 1px #000080" />
    <div>
        @{
            if (ViewBag.Viewer is Array)
            {
                // Display multiple charts
                for (int i = 0; i < ViewBag.Viewer.Length; ++i)
                {
                    @:@Html.Raw(ViewBag.Viewer[i].RenderHTML())
                }
            }
            else
            {
                // Display one chart only
                @:@Html.Raw(ViewBag.Viewer.RenderHTML())
            }
        }
    </div>
</body>
</html>