Different chart styles: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 24: Line 24:
         function lineChart() {
         function lineChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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:'line',strokeWidth:4,strokeColor:'#0000ff'});
             var a = board.createElement('chart', dataArr, {chartStyle:'line',strokeWidth:4,strokeColor:'#0000ff'});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 35: Line 33:
         function splineChart() {
         function splineChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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:'spline',strokeWidth:4,strokeColor:'#0000ff'});
             var a = board.createElement('chart', dataArr, {chartStyle:'spline',strokeWidth:4,strokeColor:'#0000ff'});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 46: Line 42:
         function barChart() {
         function barChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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',width:0.6,labels:dataArr});
             var a = board.createElement('chart', dataArr, {chartStyle:'bar',width:0.6,labels:dataArr});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 57: Line 51:
         function multiStyleChart() {
         function multiStyleChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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});
             var a = board.createElement('chart', dataArr, {chartStyle:'bar,line,point',width:0.8,style:6,labels:dataArr});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 68: Line 60:
         function twoBarCharts() {
         function twoBarCharts() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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 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});
             var b = board.createElement('chart', [[2,4,6,8],[3,1,2,5]], {chartStyle:'bar',fillColor:'#C3D9FF',width:0.8});
Line 80: Line 70:
         function horizontalBarChart() {
         function horizontalBarChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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',labels:dataArr,width:0.8,dir:'horizontal'});
             var a = board.createElement('chart', dataArr, {chartStyle:'bar',labels:dataArr,width:0.8,dir:'horizontal'});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 91: Line 79:
         function dynamicBarChart() {
         function dynamicBarChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 450, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 450, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             board.suspendUpdate();
            var b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
            var b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
             var s = board.createElement('slider', [[5,-1],[8,-1], [1,1,2]], {name:'S'});
             var s = board.createElement('slider', [[5,-1],[8,-1], [1,1,2]], {name:'S'});
             var f = [function(){return this.board.round(s.Value()*4,2);},
             var f = [function(){return this.board.round(s.Value()*4,2);},
Line 108: Line 93:
         function fitChart(deg) {
         function fitChart(deg) {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 450, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 450, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             board.suspendUpdate();
             var a = board.createElement('chart', dataArr,  
             var a = board.createElement('chart', dataArr,  
Line 127: Line 112:
         function lineChart() {
         function lineChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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:'line',strokeWidth:4,strokeColor:'#0000ff'});
             var a = board.createElement('chart', dataArr, {chartStyle:'line',strokeWidth:4,strokeColor:'#0000ff'});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 138: Line 121:
         function splineChart() {
         function splineChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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:'spline',strokeWidth:4,strokeColor:'#0000ff'});
             var a = board.createElement('chart', dataArr, {chartStyle:'spline',strokeWidth:4,strokeColor:'#0000ff'});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 149: Line 130:
         function barChart() {
         function barChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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',width:0.6,labels:dataArr});
             var a = board.createElement('chart', dataArr, {chartStyle:'bar',width:0.6,labels:dataArr});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 160: Line 139:
         function multiStyleChart() {
         function multiStyleChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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});
             var a = board.createElement('chart', dataArr, {chartStyle:'bar,line,point',width:0.8,style:6,labels:dataArr});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 171: Line 148:
         function twoBarCharts() {
         function twoBarCharts() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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 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});
             var b = board.createElement('chart', [[2,4,6,8],[3,1,2,5]], {chartStyle:'bar',fillColor:'#C3D9FF',width:0.8});
Line 183: Line 158:
         function horizontalBarChart() {
         function horizontalBarChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             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',labels:dataArr,width:0.8,dir:'horizontal'});
             var a = board.createElement('chart', dataArr, {chartStyle:'bar',labels:dataArr,width:0.8,dir:'horizontal'});
             board.unsuspendUpdate();
             board.unsuspendUpdate();
Line 194: Line 167:
         function dynamicBarChart() {
         function dynamicBarChart() {
             JXG.JSXGraph.freeBoard(board);
             JXG.JSXGraph.freeBoard(board);
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 450, unitX: 50, unitY: 50,axis:true});
             board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 450, unitX: 50, unitY: 50, axis:true});
             board.suspendUpdate();
             board.suspendUpdate();
 
             var s = board.createElement('slider', [[5,-1],[8,-1], [1,1,2]], {name:'S'});
             var s = board.createElement('slider', [[0.75,-2],[5,-2.0],[0,0,15]], {name:'S'});
             var f = [function(){return this.board.round(s.Value()*4,2);},
             var f = [function(){return this.board.round(s.Value()*4,2);},
                     function(){return this.board.round(s.Value()*(-1),2);},
                     function(){return this.board.round(s.Value()*(-1),2);},
Line 212: Line 184:
             board.suspendUpdate();
             board.suspendUpdate();
             var a = board.createElement('chart', dataArr,  
             var a = board.createElement('chart', dataArr,  
                 {chartStyle:'fit,bar', degree:deg, colorArray:['#B02B2C','#3F4C6B','#C79810','#D15600'], dash:2}
                 {chartStyle:'bar,fit', degree:deg, colorArray:['#B02B2C','#3F4C6B','#C79810','#D15600'], dash:2}
             );
             );
             board.unsuspendUpdate();
             board.unsuspendUpdate();
         }
         }
         lineChart();
         lineChart();
</source>
</source>

Revision as of 12:21, 30 September 2009

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

        var board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 400, unitX: 50, unitY: 50});

        var dataArr = [4,1,3,2,5,7,1.5,2];
        
        // Line chart
        function lineChart() {
            JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            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', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            var a = board.createElement('chart', dataArr, {chartStyle:'spline',strokeWidth:4,strokeColor:'#0000ff'});
            board.unsuspendUpdate();
        };

        // Bar chart
        function barChart() {
            JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            var a = board.createElement('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', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            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: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            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
        function horizontalBarChart() {
            JXG.JSXGraph.freeBoard(board);
            board = JXG.JSXGraph.initBoard('jxgbox', {originX: 50, originY: 500, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            var a = board.createElement('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', {originX: 50, originY: 450, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            var s = board.createElement('slider', [[5,-1],[8,-1], [1,1,2]], {name:'S'});
            var f = [function(){return this.board.round(s.Value()*4,2);},
                     function(){return this.board.round(s.Value()*(-1),2);},
                     function(){return this.board.round(s.Value()*3,2);},
                     function(){return this.board.round(s.Value()*2,2);}];
            var chart = board.createElement('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', {originX: 50, originY: 450, unitX: 50, unitY: 50, axis:true});
            board.suspendUpdate();
            var a = board.createElement('chart', dataArr, 
                {chartStyle:'bar,fit', degree:deg, colorArray:['#B02B2C','#3F4C6B','#C79810','#D15600'], dash:2}
            );
            board.unsuspendUpdate();
        }
        lineChart();