0.5.4 • Published 8 years ago

g2d-chart v0.5.4

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

License npm npm

g2.chart

g2.chart is an important brick for g2.js being an engineers and scientists graphics tool. g2.chart as a g2 addon generates x/y-line charts only, but it does that very well.

Example: first chart

g2 %amp; charts

<canvas id="c" width="250" height="175"></canvas>
<script src="./g2/g2.js"></script>
<script src="./g2.chart.js"></script>
<script>
  g2().cartesian()
      .chart({ x:35, y:35, b:200, h:120,
               funcs:[
                 { data:[-2,6,0,-2,2,1,4,1,6,5], 
                   fill:true, 
                   dots:true }
               ],
             })
      .exe(document.getElementById("c").getContext("2d"));
</script>

Chart usage

g2.chart introduces a new chart command, which expects a single javascript object specifying the chart properties. In order to use it properly you need to set g2's cartesian flag. You want to set the chart dimensions by specifying the lower left corner x and y as well as width b and height h of the rectangular chart area. Please note, that chart title, axis ticks, labels and titles are not included in that dimensions.

dimensions

Chart functions, range and style

Line charts display functional relations. These relations can be numerically represented via datasets or algebraically via functions. g2.chart offers both representations. A single chart can contain multiple functions and even mix numerical and algebraic forms.

The chart x-value range is extracted from available datasets or must be explicitly specified. Its y-value range can be derived from both datasets and algebraic functions or - again - be explicitly set.

Example: multiple functions

sintan

g2().cartesian()
    .chart({x:25,y:25,b:200,h:100,
            title:{text:"sine + tan"},
            xmin:-Math.PI/2,xmax:2*Math.PI,
            ymin:-3,ymax:3,
            funcs:[
              {fn:Math.sin,dx:Math.PI/30,fill:true},
              {fn:Math.tan,dx:Math.PI/90,fill:true}
            ]})
    .exe(ctx);

In this example the x-value range is explicitely set by xmin and xmax. It is valid then for all functions. The y-value range is also explicitely set by ymin and ymax. That is done here only, because the tan-function has singularities at odd multiples of pi/2. So its chart is simply clipped to the desired range of [-3,3].

Please note that we didn't specify a color for displaying the functions in the last example. g2.chart provides a small set of default colors for sequentially assigning them to functions without explicite color definitions. If the fill property is set to true, the function chart is filled with respect to the zero x-axis using its semi-transparent function color.

Example: Axis properties

sin+axis

g2().cartesian()
    .chart({ x:35,y:35,b:200,h:100,
             title:{ text:"sin(φ)"},
                     xmin:-Math.PI/2,xmax:2*Math.PI,
                     funcs:[
                       {fn:Math.sin,dx:Math.PI/30,fill:true}
                     ],
                     xaxis: {
                       title:"φ",
                       grid:true
                     },
                     yaxis: {
                       title:"sin(φ)",
                       origin:true
                     }
                   })
    .exe(ctx);

Chart API

After a chart object is created, we might want to interact with it then. So we would like to request canvas coordinates for certain chart area coordinates. There is a small API for supporting tasks like this.

MethodArgumentsReturnsDescription
valOf(pix)pix canvas pointobjectPoint value in chart coordinates from point in canvas coordinates.
pixOf(usr)usr chart pointobjectPoint value in canvas coordinates from point in chart coordinates.
trimPixOf(usr)usr chart pointobjectPoint value in canvas coordinates from point in chart coordinates trimmed to chart area region limits.
yOf(x)x chart x-valuefloaty-value in chart coordinates.
drawMarkersAt(g,x)g g2 instance x chart x-valueobjectDraw marker points in canvas coordinates to g2 instance g according to x-value for all functions in chart.

It is possible to place markers on chart functions after they are generated. For this we need to create a standalone chart instance via g2.Chart.create first.

Example: chart marker

chart marker

var ctx = document.getElementById("c").getContext("2d"),
    g = g2(),
    ch = g2.Chart.create({ x:100,y:100,b:200,h:100,
                           funcs:[{data:[-1,-2,3,2,6,0],fill:true}],
                           title:{text:"marker on chart"},
                           yaxis:{origin:true}
                         });
 g.cartesian()
  .chart(ch)
  .cpy(ch.drawMarkersAt(g,4))
  .exe(ctx);

Those markers can be set interactively then.

Example: interactive chart

dynamic charts

(animated gif)
see example

Markers can also get animated along simulation parameters.

Example: animated chart

dynamic charts

(animated gif)
see example

Chart Properties

We can set some properties with x- and y-axis, as axis titles, grids and an explicite origin line. It is also possible to suppress automatically defined axis ticks and labels. Custom labels are not implemented at current.

You can overwrite the the default values, if you are not comfortable with them.

NameTypeDefaultDescription
xfloat-x position of chart area's lower left corner.
yfloat-y position of chart area's lower left corner.
bfloat-breadth / width of chart area.
hfloat-height of chart area.
styleobject{ls:"transparent", fs:"#efefef"}chart border and area color style.
titlestring or objectnullchart title string or object specifying title properties.
title.textstringnullchart title string.
title.offsetfloat3vertical offset to chart area.
title.styleobject{foc:"black", foz:16, thal:"center", tval:"bottom"}title style.
funcsarray[]array of dataset data and / or function fn objects.
funcs[item]objectdataset and / or function object.
funcs[item].dataarrayarray of data points as flat array [x,y,..], array of point arrays [[x,y],..] or array of point objects [{x,y},..].
funcs[item].fnfunctionFunction y = f(x) accepting x-value returning y-value.
funcs[item].dxfloatx increment to apply to function fn. Ignored with data points.
funcs[item].fillbooleanfalseFill region between function polygon and x-origin line.
funcs[item].dotsbooleanfalsePlace circular dots at data points. Better avoid with fns.
xaxisobjectx-axis properties.
xaxis.styleobject{ls:"#888", thal:"center", tval:"top", foc:"black"}x-axis style.
xaxis.titlestring or objectx-axis title string or object specifying title properties.
xaxis.title.textstringnullx-axis title string.
xaxis.title.offsetfloat1vertical x-axis title string offset.
xaxis.title.styleobject{foz:12}x-axis title style. Not specified properties are inherited from xaxis.style.
xaxis.linebooleantruedisplay x-axis base line.
xaxis.originbooleanfalsedisplay x-axis origin line.
xaxis.ticksobjectdisplay x-axis ticks. Set to false to suppress.
xaxis.ticks.lenfloat6x-axis major ticks length.
xaxis.gridboolean or objectfalsedisplay x-axis grid using style object.
xaxis.labelsboolean or objectdisplay x-axis labels.
xaxis.labels.loc'auto''auto'calculate x-axis labels automatically.
xaxis.labels.offsetfloat1vertical x-axis labels string offset.
xaxis.labels.styleobject{foz:11}x-axis labels style. Not specified properties are inherited from xaxis.style.
yaxisobjecty-axis properties.
yaxis.styleobject{ls:"#888", thal:"center", tval:"bottom", foc:"black"}y-axis style.
yaxis.titlestring or objecty-axis title string or object specifying title properties.
yaxis.title.textstringnully-axis title string.
yaxis.title.offsetfloat2horizontal y-axis title string offset.
yaxis.title.styleobject{foz:12}y-axis title style. Not specified properties are inherited from yaxis.style.
yaxis.linebooleantruedisplay y-axis base line.
yaxis.originbooleanfalsedisplay y-axis origin line.
yaxis.ticksobjectdisplay y-axis ticks. Set to false to suppress.
yaxis.ticks.lenfloat6y-axis major ticks length.
yaxis.gridboolean or objectfalsedisplay y-axis grid using style object.
yaxis.labelsboolean or objectdisplay y-axis labels.
yaxis.labels.loc'auto''auto'calculate y-axis labels automatically.
yaxis.labels.offsetfloat1horizontal y-axis labels string offset.
yaxis.labels.styleobject{foz:11}y-axis labels style. Not specified properties are inherited from yaxis.style.

Tests

See this growing table of test cases with canvas and svg output side by side.

GitCDN

Use the link https://gitcdn.xyz/repo/goessner/g2-chart/master/g2.chart.min.js for getting the latest commit as a raw file.

In HTML use ...

<script src="https://gitcdn.xyz/repo/goessner/g2-chart/master/g2.chart.min.js"></script>

License

g2.chart is licensed under the terms of the MIT License.

Change Log

0.2.0 - 2016-07-31

Initial Commit

  • chart command.