Getting Started > SharpPlot in different Languages

SharpPlot in different Languages

SharpPlot being delivered both as a .Net assembly and as an APL workspace, it can be used in many different languages. This tutorial shows how to write the same SharpPlot script in different languages (C#, VB/.Net, Dyalog APL/.Net, Dyalog APL).


Sample chart in C#

Here’s the code to produce that chart from C#. It can be used as a Rosetta stone for users trying to write scripts in other languages.

To build this project in Visual Studio, a few steps are required :

  • Create a Console C# project in Visual Studio
  • Add a reference to System.Drawing (Project → Add Reference... → .Net)
  • Add a reference to sharpplot.dll (Project → Add Reference... → Browse)
  • Paste the following code into your main .cs file
  • Build and run
 using System;
using System.Drawing;       // Requires adding a reference to System.Drawing to the C# project
using System.Drawing.Imaging;
using Causeway;             // Requires adding a reference to sharpplot.dll to the C# project
    
namespace MyNamespace
{
  class MyClass
  {
    static void Main(string[] args)
    {
        int[] data = {33, 18, 12, 10};
        int[] exp = {0, 0, 0, 20};
        string[] key = {"UK", "France", "Italy", "Ireland"};
    
        SharpPlot sp = new SharpPlot();                               // Use default size
        sp.Heading = "Sample pie chart";                              // Property
        sp.SetKeyText(key);                                           // Simple function
        sp.KeyStyle = KeyStyles.CenterAlign|KeyStyles.BottomAlign;    // Flag property
        sp.PieChartStyle = PieChartStyles.ValueTags; 
        sp.DrawPieChart(data, exp);                                   // Multiple-argument function
       
        sp.SaveImage("samplepie.png", ImageFormat.Png);               // Save chart as PNG image at default size
        Console.WriteLine(sp.RenderSvg());                            // Output SVG image source to standard text stream
    }
  }
} 

Sample chart in VB.Net

Here’s the same script in VB.Net. You can build it in a few steps :

  • Create a Console VB.Net project in Visual Studio
  • Add a reference to System.Drawing (Project → Add Reference... → .Net)
  • Add a reference to sharpplot.dll (Project → Add Reference... → Browse)
  • Paste the following code into your main .vb file
  • Build and run
 Imports System
Imports System.Drawing   ' Requires adding a reference to System.Drawing to the VB.Net project
Imports System.Drawing.Imaging
Imports Causeway         ' Requires adding a reference to sharpplot.dll to the VB.Net project 
  
Module MyModule
    Sub Main()
        Dim data As Integer() = New Integer() {33, 18, 12, 10}
        Dim exp As Integer() = New Integer() {0, 0, 0, 20}
        Dim key As String() = New String() {"UK", "France", "Italy", "Ireland"}
   
        Dim sp As SharpPlot = New SharpPlot                                     ' Use default size
        sp.Heading = "Sample pie chart"                                         ' Property
        sp.SetKeyText(key)                                                      ' Function
        sp.KeyStyle = KeyStyles.CenterAlign+KeyStyles.BottomAlign               ' Flag property
        sp.PieChartStyle = PieChartStyles.ValueTags                             
        sp.DrawPieChart(data, exp)                                              ' Function with multiple arguments
   
        sp.SaveImage("samplepie.png", ImageFormat.Png)                          ' Save the chart as PNG image at default size
        Console.WriteLine(sp.RenderSvg())                                       ' Output SVG image source to standard text stream
    End Sub
End Module 

Sample chart in Dyalog APL/.Net

Because Dyalog has a .Net bridge, it can use sharpplot.dll directly, which is in the default Dyalog installation directory, and can be accessed by setting ⎕USING to ‘,sharpplot.dll’.

Refer to the Dyalog .Net Interface Guide if you need explanations about how to use .Net objects and pass arrays as arguments.

 ∇ {svg}←MyChart;⎕USING;data;exp;key;sp
  ⎕USING←'System.Drawing,system.drawing.dll' 'System.Drawing.Imaging' 'Causeway,sharpplot.dll'
  data←33 18 12 10
  exp←0 0 0 20
  key←'UK' 'France' 'Italy' 'Ireland'
  
  sp←⎕NEW SharpPlot                                 ⍝ Default size
  sp.Heading←'Sample pie chart'                     ⍝ Property
  sp.SetKeyText(⊂key)                               ⍝ Need to enclose the key vector because it is a single argument, rather than a list of arguments
  sp.KeyStyleKeyStyles.(CenterAlign+BottomAlign)   ⍝ Flag property
  sp.PieChartStylePieChartStyles.ValueTags        
  sp.DrawPieChart(data exp)                         ⍝ No need to enclose here because the 2-item vector maps to 2 different arguments
   
  sp.SaveImage('samplepie.png' ImageFormat.Png)     ⍝ Save the chart as a PNG image, at default size
  svg←sp.RenderSvg ⍬                                ⍝ Return SVG image source
∇ 
See the
]chart 
User Command for more samples, and a GUI interface to build chart scripts.

Sample chart in non-.Net Dyalog APL

Since Dyalog v14.0, SharpPlot is also shipped as a pure-APL workspace, than can be used on all platforms supported by Dyalog. All you need to do is to copy sharpplot.dws.

The main difference from using SharpPlot through the .Net bridge is the lack of support for raster graphics. Charts can be output only as SVG, PostScript or PDF (VML is deprecated and should not be used).

Because of the number of different classes provided by the sharpplot workspace, you probably want to bring them in a parent namespace for charting.

      ⎕CS 'Causeway' ⎕NS '' ⋄⎕CY 'sharpplot.dws'   ⍝ copy all classes in a single namespace called "Causeway"
 ∇ {svg}←MyChart;data;exp;key;sp
    data←33 18 12 10
    exp←0 0 0 20
    key←'UK' 'France' 'Italy' 'Ireland'
   
    sp←⎕NEW SharpPlot                                 ⍝ default size
    sp.Heading←'Sample pie chart'                     ⍝ Property
    sp.SetKeyText(⊂key)                               ⍝ better enclose it for interoperability with SharpPlot/.Net
    sp.KeyStyleKeyStyles.(CenterAlign+BottomAlign)   ⍝ Flag property
    sp.PieChartStylePieChartStyles.ValueTags 
    sp.DrawPieChart(data exp)                         ⍝ Two arguments
   
    sp.SaveSvg(⊂'samplepie.svg')                      ⍝ SharpPlot/APL doesn't support raster graphics : SVG is the recommended way to export charts.
    svg←sp.RenderSvg ⍬                                ⍝ Return SVG image source
∇
 

See the #.Samples namespace in sharpplot.dws for more example scripts.

See also ...

Getting Started | SharpPlot Tutorials | SharpLeaf Tutorials | Installing SharpPlot | SharpPlot Members


Send comments on this topic
© Dyalog Ltd 2016