tags:

views:

94

answers:

2

I'm relatively new to Javascript, and although I know how to use it, I don't really understand the mechanics behind it. Bear with me here.

I need to write a small app that creates a chart (in SVG) based on data I take in as an XML file. I found PlotKit, which does exactly what I need, except that it's written in Javascript, while my current program is written in c#. I did some googling and found a few articles which explain how to evaluate simple Javascript code using the .NET VsaEngine class. Unfortunately, I have absolutely no idea how to use the VsaEngine to execute more complicated Javascript that requires references to other files. Basically, all I want is for c# to be able to call something like this as Javascript:

var layout = new PlotKit.Layout("bar", {});
layout.addDataset("data", [[0, 0], [1, 1], [2, 2]]);
layout.evaluate();
var canvas = MochiKit.DOM.getElement("graph");
var plotter = new PlotKit.SVGRenderer(canvas, layout, {});
var svg = SVGRenderer.SVG();

And get back the SVG string for the chart. I have no idea how to make it so that the above script knows where to look for all of the necessary objects. If I were to make a web page to do this, I would just add a few script headers referencing /plotkit/Layout.js, /plotkit/Canvas.js, etc., the Javascript would work fine.

If anyone could explain exactly how I would use PlotKit through C#, or could explain a more effective way to do this, I would really appreciate it.

EDIT: I realize I wasn't too clear with this question - I need my c# program to emulate a Javascript engine and use the PlotKit library without actually running a web browser. Is there any way to do this?

A: 

PlotKit is a JavaScript library that is intended to execute in the Client's Web Browser. C# is executed on the Server. To go about communicating between the two, you would render whatever data you wish to pass to PlotKit on the server and then output it in the HTML you send to the client.

So in your C# codebehind you would construct the JSON object that would be passed to PlotKit's addDataset method.

...
    public partial class Default : System.Web.UI.Page
    {
        protected string PlotKitData = "[]";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) PlotKitData = GenerateJSON();
...

Then in your ASPX codefront you would have something like this.

<script>
    var layout = new PlotKit.Layout("bar", {});
    layout.addDataset("data", <%=PlotKitData%>);
    layout.evaluate();
    var canvas = MochiKit.DOM.getElement("graph");
    var plotter = new PlotKit.SVGRenderer(canvas, layout, {});
    var svg = SVGRenderer.SVG();
</script>
CptSkippy
Maybe I wasn't very clear with my question - I need my c# program to emulate a Javascript engine and use the PlotKit library without actually running a web browser. Is there any way to do this?
PlotKit was intended to run in a brower client side and required the MochiKit framework.
CptSkippy
A: 

Perhaps ZedGraph might suit your needs instead?

ZedGraph, unless I missed something, can't save charts as SVG files, which is critical in my case.I've found a free Java library that can, but I'm still curious to see if a solution exists/