Different chart styles: Difference between revisions

From JSXGraph Wiki
(New page: <html> <link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" /> <script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/pro...)
 
No edit summary
 
(22 intermediate revisions by 3 users not shown)
Line 1: Line 1:
JSXGraph supports chart plotting.  Here are some examples of possible chart types.
<html>
<html>
<link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" />
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
   <p>
   <p>
     <input type="button" value="Line chart" onClick="lineChart()" style='margin:1em'><br>
     <input type="button" value="Line chart" onClick="lineChart()">
     <input type="button" value="Spline chart" onClick="splineChart()" style='margin:1em'><br>
     <input type="button" value="Spline chart" onClick="splineChart()">
     <input type="button" value="Bar chart" onClick="barChart()" style='margin:1em'><br>
     <input type="button" value="Bar chart" onClick="barChart()">
     <input type="button" value="Multiple styles" onClick="multiStyleChart()" style='margin:1em'><br>
     <input type="button" value="Multiple styles" onClick="multiStyleChart()">
     <input type="button" value="Two bar charts" onClick="twoBarCharts()" style='margin:1em'><br>
     <input type="button" value="Two bar charts" onClick="twoBarCharts()">
     <input type="button" value="Horizontal bars" onClick="horizontalBarChart()" style='margin:1em'><br>
     <input type="button" value="Horizontal bars" onClick="horizontalBarChart()">
     <input type="button" value="Dynamic bars" onClick="dynamicBarChart()" style='margin:1em'><br>
     <input type="button" value="Dynamic bars" onClick="dynamicBarChart()">
     <input type="button" value="Pie chart" onClick="pieChart()" style='margin:1em'><br>
    <input type="button" value="Regression line" onClick="fitChart(1)">
     <input type="button" value="Regression parabola" onClick="fitChart(2)">
   </p>
   </p>
<div id="jxgbox" class="jxgbox" style="width:600px; height:600px;"></div>
</html>
<script language="JavaScript">
<jsxgraph width="600" height="600" box="jxgbox">
        var board = JXG.JSXGraph.initBoard('jxgbox', {originX: 350, originY: 400, unitX: 50, unitY: 50});
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
var dataArr = [4,1,3,2,5,7,1.5,2];
       
// Line chart
function lineChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'line',strokeWidth:4,strokeColor:'#0000ff'});
    board.unsuspendUpdate();
};
 
// Line chart with cubic splines
function splineChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'spline',strokeWidth:4,strokeColor:'#0000ff'});
    board.unsuspendUpdate();
};
 
// Bar chart
function barChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'bar',width:0.6,labels:dataArr});
    board.unsuspendUpdate();
};
 
// Single chart with multiple styles
function multiStyleChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'bar,line,point',width:0.8,size:4,labels:dataArr});
    board.unsuspendUpdate();
};
 
// Two bar charts
function twoBarCharts() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', [[1,3,5,7],[4,-1,3,2]], {chartStyle:'bar',width:0.8});
    var b = board.create('chart', [[2,4,6,8],[3,1,2,5]], {chartStyle:'bar',fillColor:'#C3D9FF',width:0.8});
    board.unsuspendUpdate();
};
 
// Bar chart with horizontal bars
function horizontalBarChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'bar',labels:dataArr,width:0.8,dir:'horizontal'});
    board.unsuspendUpdate();
};
 
// Single chart with dynamic entries
function dynamicBarChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var s = board.create('slider', [[5,-1],[8,-1], [1,1,2]], {name:'S'});
    var f = [function(){return parseFloat((s.Value()*4).toFixed(2));},
            function(){return parseFloat((s.Value()*(-1)).toFixed(2));},
            function(){return parseFloat((s.Value()*3).toFixed(2));},
            function(){return parseFloat((s.Value()*2).toFixed(2));}];
    var chart = board.create('chart', [f], {chartStyle:'bar',width:0.8,labels:f});
    board.unsuspendUpdate();
};
 
// Regression curve
function fitChart(deg) {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr,
        {chartStyle:'bar,fit', degree:deg, colorArray:['#B02B2C','#3F4C6B','#C79810','#D15600'], dash:2}
    );
    board.unsuspendUpdate();
}
lineChart();
</jsxgraph>
 
==The Code==


        var dataArr = [4,1,3,2,5,7,1.5,2];
<source lang="javascript">
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
var dataArr = [4,1,3,2,5,7,1.5,2];
          
          
        // Line chart
// Line chart
        function lineChart() {
function lineChart() {
            JXG.JSXGraph.freeBoard(board);
    JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 500, unitX: 50, unitY: 50});
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
            board.suspendUpdate();
    board.suspendUpdate();
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
    var a = board.create('chart', dataArr, {chartStyle:'line',strokeWidth:4,strokeColor:'#0000ff'});
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
    board.unsuspendUpdate();
            var a = board.createElement('chart', dataArr, {chartStyle:'line',strokeWidth:4,strokeColor:'#0000ff'});
};
            board.unsuspendUpdate();
 
        };
// Line chart with cubic splines
function splineChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'spline',strokeWidth:4,strokeColor:'#0000ff'});
    board.unsuspendUpdate();
};


        function splineChart() {
// Bar chart
            JXG.JSXGraph.freeBoard(board);
function barChart() {
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 500, unitX: 50, unitY: 50});
    JXG.JSXGraph.freeBoard(board);
            board.suspendUpdate();
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
    board.suspendUpdate();
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
    var a = board.create('chart', dataArr, {chartStyle:'bar',width:0.6,labels:dataArr});
            var a = board.createElement('chart', dataArr, {chartStyle:'spline',strokeWidth:4,strokeColor:'#0000ff'});
    board.unsuspendUpdate();
            board.unsuspendUpdate();
};
        };


        // Bar chart
// Single chart with multiple styles
        function barChart() {
function multiStyleChart() {
            JXG.JSXGraph.freeBoard(board);
    JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 500, unitX: 50, unitY: 50});
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
            board.suspendUpdate();
    board.suspendUpdate();
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
    var a = board.create('chart', dataArr, {chartStyle:'bar,line,point',width:0.8,size:4,labels:dataArr});
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
    board.unsuspendUpdate();
            var a = board.createElement('chart', dataArr, {chartStyle:'bar',width:0.6,labels:dataArr});
};
            board.unsuspendUpdate();
        };
        //var s = board.createElement('chart', [4,1], {chartStyle:'bar',strokeWidth:8,width:0.4,strokeColor:'#ff0000'});  // strokeWidth in Polygon???
       
        // Single chart with multiple styles
        function multiStyleChart() {
            JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 500, unitX: 50, unitY: 50});
            board.suspendUpdate();
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
            var a = board.createElement('chart', dataArr, {chartStyle:'bar,line,point',width:0.8,style:6,labels:dataArr});
            board.unsuspendUpdate();
        };
       
        // Two bar charts
        function twoBarCharts() {
            JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 500, unitX: 50, unitY: 50});
            board.suspendUpdate();
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
            var a = board.createElement('chart', [[1,3,5,7],[4,-1,3,2]], {chartStyle:'bar',width:0.8});
            var b = board.createElement('chart', [[2,4,6,8],[3,1,2,5]], {chartStyle:'bar',fillColor:'#C3D9FF',width:0.8});
            board.unsuspendUpdate();
        };


        // Bar chart with horizontal bars
// Two bar charts
        function horizontalBarChart() {
function twoBarCharts() {
            JXG.JSXGraph.freeBoard(board);
    JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 500, unitX: 50, unitY: 50});
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
            board.suspendUpdate();
    board.suspendUpdate();
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
    var a = board.create('chart', [[1,3,5,7],[4,-1,3,2]], {chartStyle:'bar',width:0.8});
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
    var b = board.create('chart', [[2,4,6,8],[3,1,2,5]], {chartStyle:'bar',fillColor:'#C3D9FF',width:0.8});
            var a = board.createElement('chart', dataArr, {chartStyle:'bar',labels:dataArr,width:0.8,dir:'horizontal'});
    board.unsuspendUpdate();
            board.unsuspendUpdate();
};
        };


        // Single chart with dynamic entries
// Bar chart with horizontal bars
        function dynamicBarChart() {
function horizontalBarChart() {
            JXG.JSXGraph.freeBoard(board);
    JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 450, unitX: 50, unitY: 50});
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
            board.suspendUpdate();
    board.suspendUpdate();
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
    var a = board.create('chart', dataArr, {chartStyle:'bar',labels:dataArr,width:0.8,dir:'horizontal'});
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
    board.unsuspendUpdate();
};


            var s = board.createElement('slider', [5,-1,3,1,2,1], {name:'S'});
// Single chart with dynamic entries
            var f = [function(){return this.board.round(s.X()*4,2);},
function dynamicBarChart() {
                    function(){return this.board.round(s.X()*(-1),2);},
    JXG.JSXGraph.freeBoard(board);
                    function(){return this.board.round(s.X()*3,2);},
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
                    function(){return this.board.round(s.X()*2,2);}];
    board.suspendUpdate();
            var chart = board.createElement('chart', [f], {chartStyle:'bar',width:0.8,labels:f});
    var s = board.create('slider', [[5,-1],[8,-1], [1,1,2]], {name:'S'});
            board.unsuspendUpdate();
    var f = [function(){return parseFloat((s.Value()*4).toFixed(2));},
        };
            function(){return parseFloat((s.Value()*(-1)).toFixed(2));},
            function(){return parseFloat((s.Value()*3).toFixed(2));},
            function(){return parseFloat((s.Value()*2).toFixed(2));}];
    var chart = board.create('chart', [f], {chartStyle:'bar',width:0.8,labels:f});
    board.unsuspendUpdate();
};


        // Single pie chart
        function pieChart() {
            JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 250, originY: 450, unitX: 50, unitY: 50});
            board.suspendUpdate();
            var a = board.createElement('chart', dataArr,
                {chartStyle:'pie', colorArray:['#B02B2C','#3F4C6B','#C79810','#D15600'],fillOpacity:0.9,center:[5,2]}
            );
            board.unsuspendUpdate();
        }
</script>
</html>


<source lang="javascript">
// Regression curve
function fitChart(deg) {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr,
        {chartStyle:'bar,fit', degree:deg, colorArray:['#B02B2C','#3F4C6B','#C79810','#D15600'], dash:2}
    );
    board.unsuspendUpdate();
}
lineChart();
</source>
</source>


[[Category:Examples]]
[[Category:Examples]]
[[Category:Charts]]

Latest revision as of 14:54, 20 February 2013

JSXGraph supports chart plotting. Here are some examples of possible chart types.

The Code

var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
var dataArr = [4,1,3,2,5,7,1.5,2];
        
// Line chart
function lineChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'line',strokeWidth:4,strokeColor:'#0000ff'});
    board.unsuspendUpdate();
};

// Line chart with cubic splines
function splineChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'spline',strokeWidth:4,strokeColor:'#0000ff'});
    board.unsuspendUpdate();
};

// Bar chart
function barChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'bar',width:0.6,labels:dataArr});
    board.unsuspendUpdate();
};

// Single chart with multiple styles
function multiStyleChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'bar,line,point',width:0.8,size:4,labels:dataArr});
    board.unsuspendUpdate();
};

// Two bar charts
function twoBarCharts() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', [[1,3,5,7],[4,-1,3,2]], {chartStyle:'bar',width:0.8});
    var b = board.create('chart', [[2,4,6,8],[3,1,2,5]], {chartStyle:'bar',fillColor:'#C3D9FF',width:0.8});
    board.unsuspendUpdate();
};

// Bar chart with horizontal bars
function horizontalBarChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, {chartStyle:'bar',labels:dataArr,width:0.8,dir:'horizontal'});
    board.unsuspendUpdate();
};

// Single chart with dynamic entries
function dynamicBarChart() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var s = board.create('slider', [[5,-1],[8,-1], [1,1,2]], {name:'S'});
    var f = [function(){return parseFloat((s.Value()*4).toFixed(2));},
             function(){return parseFloat((s.Value()*(-1)).toFixed(2));},
             function(){return parseFloat((s.Value()*3).toFixed(2));},
             function(){return parseFloat((s.Value()*2).toFixed(2));}];
    var chart = board.create('chart', [f], {chartStyle:'bar',width:0.8,labels:f});
    board.unsuspendUpdate();
};


// Regression curve
function fitChart(deg) {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 10, 11, -2], axis: true});
    board.suspendUpdate();
    var a = board.create('chart', dataArr, 
        {chartStyle:'bar,fit', degree:deg, colorArray:['#B02B2C','#3F4C6B','#C79810','#D15600'], dash:2}
    );
    board.unsuspendUpdate();
}
lineChart();