0

I have two questions regarding dojo charts.

1) How can I show point values in a dojo Chart on mouse over? Below is the chart I developed using YUI library. you can see when I mouseover a point it displays the series name and its value at the point. In this case it is Customs Duty and Fees [value: 30,546]. My question is how I can achieve this functionality in dojo charts?

enter image description here

2) Is there any way that the chart displayed on screen can be exported to an Image file (png or gif)? in Yui we can right click the chart and export it to png.

I am using the dojo version 1.8.3

You may look into the following code to see how I am creating a chart:

require(["dojo/ready", "dojox/charting/Chart2D","dojox/charting/themes/Claro"],                  function(ready, Chart,ClaroTheme) {
    ready(function() {
        var mychart = Chart("mychart");

        mychart.title = "My Chart";
        mychart.titleFont = "tahoma";
        mychart.addPlot("line_plot", {
            type: "Lines",
            lines: true,
            areas: false,
            markers: true
        });

        mychart.addPlot("column_plot", {
            type: "Columns",
            lines: true,
            areas: false,
            markers: true
        });

        mychart.addAxis("x", {
            vertical: false
        });

        mychart.addAxis("y", {
            vertical: true
        });

        mychart.addSeries("line_series", [1, 3, 5, 2, 6, 1, 0, 4, 6, 4, 1], {
            plot: "line_plot"
        });

        mychart.addSeries("column_series", [1, 3, 5, 2, 6, 1, 0, 4, 6, 4, 1], {
            plot: "column_plot"
        });
        mychart.setTheme(ClaroTheme);

        mychart.render();
    });
});

http://jsfiddle.net/5VYhN/

asim-ishaq
  • 2,190
  • 5
  • 32
  • 55

3 Answers3

2

In addition to the Tooltip where you have to mouse over the exact marker on the line, you can also use the MouseIndicator, which is really nifty. You can see it in action here:

http://demos.dojotoolkit.org/demos/mobileCharting/demo.html

Implementation is very easy:

new MouseIndicator(this.twoDimensionChart, "default", {
    series: "Plot",
    mouseOver: true,
    fillFunc: function(v) {
        return "#BFD6EB"
    },
    labelFunc: lang.hitch(this, function(v) {
        this.currentMouseIndicatorValue = v;
        return v.y;
    })
});

Note that this code is in a custom widget of mine. I have mouseOver set to true, so you don't have to click and drag like in the dojo example in the link... if you just mouse over, you get the value. I added the line to the labelFunc to set an instance variable to the current value used for the label (basically just holds the x and y values). I then created an onclick event listener for my custom widget so I can do some custom processing and display a dialog with extra info anytime somebody clicks anywhere on the chart:

this.on("click", lang.hitch(this, function(evt){
    this.popupResultsDialogForItem(this.currentMouseIndicatorValue.x);
}));

Obviously that's optional, but it just gives you the flexibility to add onclick functionality and reference whatever value is currently selected on the chart.

Bal
  • 2,027
  • 4
  • 25
  • 51
1

You can customize you mouse over by using tooltip in your data. For example:

In require statment, add "dojox/charting/action2d/Tooltip" In your chart data add 'tooltip' with text to display on mouse over

JSON chart data:

var chartdata = [{x: 8,y:"0",tooltip:"What to show during mouse over"}];

JS code to use tooltip in your chart data:

// Create the tooltip which will show during mouse over
var tip = new Tooltip(chart,"default");

// Render the chart!
chart.render();

That is all you need...Not sure about your second question...

This is easy stuff, visit this charting example page: http://dojotoolkit.org/documentation/tutorials/1.8/charting/

Using your existing example, here is how you add mouse over:

require(["dojo/ready", "dojox/charting/Chart2D","dojox/charting/themes/Claro","dojox/charting/action2d/Tooltip",], function(ready, Chart,ClaroTheme,Tooltip) {
    ready(function() {
        var mychart = Chart("mychart");

        mychart.title = "My Chart";
        mychart.titleFont = "tahoma";
        mychart.addPlot("line_plot", {
            type: "Lines",
            lines: true,
            areas: false,
            markers: true
        });

        mychart.addPlot("column_plot", {
            type: "Columns",
            lines: true,
            areas: false,
            markers: true
        });

        mychart.addAxis("x", {
             vertical: false
        });

        mychart.addAxis("y", {
            vertical: true
        });

        var column_data = [{y:1,x:1,tooltip: "column 1"}, {y: 3,x:2,tooltip: "column 2"}, {y:5,x:3,tooltip: "column 3"}, {y:2,x:4,tooltip: "column 4"}, {y:6,x:5,tooltip: "columnt 5"}, {y:1,x:6,tooltip: "column 6"}, {y:0,x:7,tooltip: "column 7"}, {y:4,x:8,tooltip: "column 8"}, {y:6,x:9,tooltip: "column 9"}, {y:4,x:10,tooltip: "column 10"}, {y:1,x:11,tooltip: "column 11"}];

        var bar_data = [{y:1,x:1,tooltip: "bar 1"}, {y: 3,x:2,tooltip: "bar 2"}, {y:5,x:3,tooltip: "bar 3"}, {y:2,x:4,tooltip: "bar 4"}, {y:6,x:5,tooltip: "bar 5"}, {y:1,x:6,tooltip: "bar 6"}, {y:0,x:7,tooltip: "bar 7"}, {y:4,x:8,tooltip: "bar 8"}, {y:6,x:9,tooltip: "bar 9"}, {y:4,x:10,tooltip: "bar 10"}, {y:1,x:11,tooltip: "bar 11"}];

        mychart.addSeries("line_series", bar_data, {
            plot: "line_plot"
        });

        mychart.addSeries("column_series", column_data, {
            plot: "column_plot"
        });
        mychart.setTheme(ClaroTheme);

        var tip = new Tooltip(mychart,"line_plot");
        var tip1 = new Tooltip(mychart,"column_plot");

        mychart.render();
    });
});
GoinOff
  • 1,792
  • 1
  • 18
  • 37
  • Thanks, but I am using the addSeries method to add data to chart. See how can i add tooltip in the following scenario: http://jsfiddle.net/5VYhN/ – asim-ishaq May 07 '13 at 10:06
  • 1
    Updated your example adding tooltip to the chart data. See revised post above... – GoinOff May 07 '13 at 14:12
  • Yes i have checked the new code. Its working, but the tooltip is displayed at bottom, can it be shown at mouse cursor position? See the updated code here: http://jsfiddle.net/5VYhN/1/ – asim-ishaq May 07 '13 at 18:20
  • I noticed that too. I thinks it's a jsfiddle thing. body tag should have class=claro on it and I think you need a to your html..See the example page for charting I posted and it should help you out – GoinOff May 07 '13 at 19:49
  • Not sure how to properly link in the claro.css sheet in jsfiddle but when I pasted the contents into the css section on jsfiddle it fixed it a bit. If you can link in the stylesheet properly, it should work the way you want... still not displaying 100% properly but close now... http://jsfiddle.net/goinoff/5VYhN/2/ – GoinOff May 07 '13 at 20:06
  • +1 I have modified my source on local machine and it is working perfect now. Appearance is also good in jsfiddle. Thanks for helping. Now i have to devise a method through which I can export the chart into an image, you may share if you have any idea. – asim-ishaq May 07 '13 at 20:59
  • Glad I could help. Check out this post for possible solution to your image problem.. http://stackoverflow.com/questions/839216/create-an-image-of-a-div-in-javascript-gif-png .. Eugene's solution sounds good too. – GoinOff May 07 '13 at 21:20
1

There is no direct support to save a chart as an image. Any dojox.gfx surface (including a chart) can be saved in a JSON format, or an SVG format. Tools for that can be found in dojox/gfx/utils.js. If your surface is done using the Canvas renderer, then you can export it as a raster image (e.g., .png) using normal non-specific to Dojo ways. I didn't check, but a canvas object may support saving as an image, if a user right-click on it.

A small cheat-sheet below:

How to get a surface from a chart:

var chart = ...;
var surface = chart.surface;

How to get a canvas element from a surface:

var canvas = surface.rawNode;

How to create an image from a canvas:

var image = new Image();
image.src = canvas.toDataUrl("image/png");

How to convert a surface to JSON, which can be send to a server:

var jsonString = dojox.gfx.utils.toJson(surface);

How to convert a surface to SVG, which can be send to a server:

var def = dojox.gfx.utils.toSvg(surface); // returns dojo.Deferred
def.then(function(svgText){
  console.log(svgText);
});
Eugene Lazutkin
  • 43,776
  • 8
  • 49
  • 56
  • As per your code snippet i had been able to get both JSON and SVG string for the chart. But the svgString in your code returns an object rather than a string. Also can you tell me how I can convert this SVG or JSON string to image on server side? Is there any utility for this in php? See the following code for the chart: http://jsfiddle.net/HAfSd/5/ – asim-ishaq May 08 '13 at 09:06
  • 1
    @asim-ishaq: my bad, toSvg() returns a deferred, because it can be a long operation that takes time and better be ran asynchronously. I updated the snippet. Many users use Batik (http://en.wikipedia.org/wiki/Batik_(software)) to convert SVG to whatever they like server-side. – Eugene Lazutkin May 09 '13 at 08:50
  • Ok thanks that worked but there is one issue. The SVG string i get from surcface does not contain the chart and axis title. I created a re-print button where in the SVG string is written to another DIV. You can see the difference visually. See http://jsfiddle.net/HAfSd/7/ – asim-ishaq May 09 '13 at 10:03
  • @asim-ishaq I updated your code: http://jsfiddle.net/HAfSd/9/ - you didn't turn off HTML labels, and some other minor things. Yet the title is still HTML (and can't be copied as SVG), so I think it is a bug, which you should file with Dojo. As a workaround, you can add a title yourself using regular dojox.gfx tools. – Eugene Lazutkin May 10 '13 at 07:03