Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

154
LINES

< > BotCompany Repo | #1001956 // LineChartExample (charts4j demo)

Java source code

1  
import static com.googlecode.charts4j.Color.*;
2  
import static com.googlecode.charts4j.UrlUtil.normalize;
3  
import static org.junit.Assert.assertEquals;
4  
5  
import java.util.Arrays;
6  
import java.util.logging.Level;
7  
import java.util.logging.Logger;
8  
9  
import org.junit.BeforeClass;
10  
import org.junit.Test;
11  
12  
/**
13  
 *
14  
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
15  
 */
16  
public class LineChartExample {
17  
18  
    @BeforeClass
19  
    public static void setUpBeforeClass() throws Exception {
20  
        Logger.global.setLevel(Level.ALL);
21  
    }
22  
23  
    @Test
24  
    public void example1() {
25  
26  
        // EXAMPLE CODE START
27  
28  
        // Defining lines
29  
        final int NUM_POINTS = 25;
30  
        final double[] competition = new double[NUM_POINTS];
31  
        final double[] mywebsite = new double[NUM_POINTS];
32  
        for (int i = 0; i < NUM_POINTS; i++) {
33  
            competition[i] = 100-(Math.cos(30*i*Math.PI/180)*10 + 50)*i/20;
34  
            mywebsite[i] = (Math.cos(30*i*Math.PI/180)*10 + 50)*i/20;
35  
        }
36  
        Line line1 = Plots.newLine(Data.newData(mywebsite), Color.newColor("CA3D05"), "My Website.com");
37  
        line1.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
38  
        line1.addShapeMarkers(Shape.DIAMOND, Color.newColor("CA3D05"), 12);
39  
        line1.addShapeMarkers(Shape.DIAMOND, Color.WHITE, 8);
40  
        Line line2 = Plots.newLine(Data.newData(competition), SKYBLUE, "Competition.com");
41  
        line2.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
42  
        line2.addShapeMarkers(Shape.DIAMOND, SKYBLUE, 12);
43  
        line2.addShapeMarkers(Shape.DIAMOND, Color.WHITE, 8);
44  
45  
46  
        // Defining chart.
47  
        LineChart chart = GCharts.newLineChart(line1, line2);
48  
        chart.setSize(600, 450);
49  
        chart.setTitle("Web Traffic|(in billions of hits)", WHITE, 14);
50  
        chart.addHorizontalRangeMarker(40, 60, Color.newColor(RED, 30));
51  
        chart.addVerticalRangeMarker(70, 90, Color.newColor(GREEN, 30));
52  
        chart.setGrid(25, 25, 3, 2);
53  
54  
        // Defining axis info and styles
55  
        AxisStyle axisStyle = AxisStyle.newAxisStyle(WHITE, 12, AxisTextAlignment.CENTER);
56  
        AxisLabels xAxis = AxisLabelsFactory.newAxisLabels("Nov", "Dec", "Jan", "Feb", "Mar");
57  
        xAxis.setAxisStyle(axisStyle);
58  
        AxisLabels xAxis2 = AxisLabelsFactory.newAxisLabels("2007", "2007", "2008", "2008", "2008");
59  
        xAxis2.setAxisStyle(axisStyle);
60  
        AxisLabels yAxis = AxisLabelsFactory.newAxisLabels("", "25", "50", "75", "100");
61  
        AxisLabels xAxis3 = AxisLabelsFactory.newAxisLabels("Month", 50.0);
62  
        xAxis3.setAxisStyle(AxisStyle.newAxisStyle(WHITE, 14, AxisTextAlignment.CENTER));
63  
        yAxis.setAxisStyle(axisStyle);
64  
        AxisLabels yAxis2 = AxisLabelsFactory.newAxisLabels("Hits", 50.0);
65  
        yAxis2.setAxisStyle(AxisStyle.newAxisStyle(WHITE, 14, AxisTextAlignment.CENTER));
66  
        yAxis2.setAxisStyle(axisStyle);
67  
68  
        // Adding axis info to chart.
69  
        chart.addXAxisLabels(xAxis);
70  
        chart.addXAxisLabels(xAxis2);
71  
        chart.addXAxisLabels(xAxis3);
72  
        chart.addYAxisLabels(yAxis);
73  
        chart.addYAxisLabels(yAxis2);
74  
75  
        // Defining background and chart fills.
76  
        chart.setBackgroundFill(Fills.newSolidFill(Color.newColor("1F1D1D")));
77  
        LinearGradientFill fill = Fills.newLinearGradientFill(0, Color.newColor("363433"), 100);
78  
        fill.addColorAndOffset(Color.newColor("2E2B2A"), 0);
79  
        chart.setAreaFill(fill);
80  
        String url = chart.toURLString();
81  
        // EXAMPLE CODE END. Use this url string in your web or
82  
        // Internet application.
83  
        Logger.global.info(url);
84  
        String expectedString = "http://chart.apis.google.com/chart?chco=CA3D05,87CEEB&chd=e:AAB4DhEzFxGnHrJRLhOZRmUpXCYZYpYAXCWfXCZIczhmmtrKuE,..-H8e7M6O5Y4U2u0exmuZrWo9nmnWn.o9pgo9m3jMeZZSU1R7&chdl=My+Website.com|Competition.com&chf=bg,s,1F1D1D|c,lg,0,363433,1.0,2E2B2A,0.0&chg=25.0,25.0,3,2&chls=3,1,0|3,1,0&chm=r,FF00004C,0,0.40,0.60|R,0080004C,0,0.70,0.90|d,CA3D05,0,-1,12,0|d,FFFFFF,0,-1,8,0|d,87CEEB,1,-1,12,0|d,FFFFFF,1,-1,8,0&chs=600x450&cht=lc&chts=FFFFFF,14&chtt=Web+Traffic%7C%28in+billions+of+hits%29&chxl=0:||25|50|75|100|1:|Hits|2:|Nov|Dec|Jan|Feb|Mar|3:|2007|2007|2008|2008|2008|4:|Month&chxp=1,50.0|4,50.0&chxr=1,0.0,100.0|4,0.0,100.0&chxs=0,FFFFFF,12,0|1,FFFFFF,12,0|2,FFFFFF,12,0|3,FFFFFF,12,0|4,FFFFFF,14,0&chxt=y,y,x,x,x";
85  
        assertEquals("Junit error", normalize(expectedString), normalize(url));
86  
    }
87  
88  
    @Test
89  
    public void example2() {
90  
        // EXAMPLE CODE START
91  
        // Defining Line
92  
        final double[] sp500 = { 62.960, 74.560, 84.300, 92.200, 95.890, 103.800, 91.600, 92.270, 96.870, 116.930, 97.540, 67.160, 89.770, 106.880, 94.750, 96.280, 107.840, 135.330, 122.300, 140.340, 164.860, 166.260, 210.680, 243.370,
93  
                247.840, 277.080, 350.680, 328.710, 415.140, 438.820, 468.660, 460.920, 614.120, 753.850, 970.840, 1231.93, 1464.47, 1334.22, 1161.02, 879.390, 1109.64, 1213.55, 1258.17, 1424.71, 1475.25 };
94  
        final double INFLATION = 0.035;
95  
96  
        final double[] inflation = new double[sp500.length];
97  
        inflation[0] = sp500[0];
98  
        for (int i = 1; i < inflation.length; i++) {
99  
            inflation[i] = inflation[i-1] *INFLATION + inflation[i-1];
100  
        }
101  
102  
        Line line1 = Plots.newLine(DataUtil.scaleWithinRange(0,1500,sp500), YELLOW, "S & P 500");
103  
        line1.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
104  
        line1.addShapeMarkers(Shape.CIRCLE, YELLOW, 10);
105  
        line1.addShapeMarkers(Shape.CIRCLE, BLACK, 7);
106  
        line1.addShapeMarker(Shape.VERTICAL_LINE_PARTIAL, BLUE,3,8);
107  
        line1.addShapeMarker(Shape.VERTICAL_LINE_PARTIAL, BLUE,3,17);
108  
        line1.addShapeMarker(Shape.VERTICAL_LINE_PARTIAL, BLUE,3,24);
109  
        line1.addShapeMarker(Shape.VERTICAL_LINE_PARTIAL, BLUE,3,40);
110  
        line1.setFillAreaColor(LIGHTYELLOW);
111  
112  
        Line line2 = Plots.newLine(DataUtil.scaleWithinRange(0,1500,inflation), LIMEGREEN, "Inflation");
113  
        line2.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
114  
        line2.addShapeMarkers(Shape.CIRCLE, LIME, 10);
115  
        line2.addShapeMarkers(Shape.CIRCLE, BLACK, 7);
116  
        line2.setFillAreaColor(LIGHTGREEN);
117  
118  
119  
        // Defining chart.
120  
        LineChart chart = GCharts.newLineChart(line1,line2);
121  
        chart.setSize(600, 450);
122  
        chart.setTitle("S & P 500|1962 - 2008", WHITE, 14);
123  
124  
        // Defining axis info and styles
125  
        AxisStyle axisStyle = AxisStyle.newAxisStyle(WHITE, 12, AxisTextAlignment.CENTER);
126  
        AxisLabels yAxis = AxisLabelsFactory.newNumericRangeAxisLabels(0, sp500[sp500.length-1]);
127  
        yAxis.setAxisStyle(axisStyle);
128  
        AxisLabels xAxis1 = AxisLabelsFactory.newAxisLabels(Arrays.asList("Fed Chiefs:","Burns","Volcker","Greenspan","Bernanke"), Arrays.asList(5,18,39,55,92));
129  
        xAxis1.setAxisStyle(axisStyle);
130  
        AxisLabels xAxis2 = AxisLabelsFactory.newNumericRangeAxisLabels(1962, 2008);
131  
        xAxis2.setAxisStyle(axisStyle);
132  
        AxisLabels xAxis3 = AxisLabelsFactory.newAxisLabels("Year", 50.0);
133  
        xAxis3.setAxisStyle(AxisStyle.newAxisStyle(WHITE, 14, AxisTextAlignment.CENTER));
134  
135  
        // Adding axis info to chart.
136  
        chart.addYAxisLabels(yAxis);
137  
        chart.addXAxisLabels(xAxis1);
138  
        chart.addXAxisLabels(xAxis2);
139  
        chart.addXAxisLabels(xAxis3);
140  
        chart.setGrid(100, 6.78, 5, 0);
141  
142  
        // Defining background and chart fills.
143  
        chart.setBackgroundFill(Fills.newSolidFill(BLACK));
144  
        chart.setAreaFill(Fills.newSolidFill(Color.newColor("708090")));
145  
        String url = chart.toURLString();
146  
        // EXAMPLE CODE END. Use this url string in your web or
147  
        // Internet application.
148  
        Logger.global.info(url);
149  
        Logger.global.info(url.length() + "");
150  
        String expectedString = "http://chart.apis.google.com/chart?chf=bg,s,000000|c,s,708090&chs=600x450&chd=e:CsDMDmD8EGEbD6D8EIE.EKC3D1EkEDEHEmFxFOF.HCHGI.KYKlL0O9OBRtSuT.TqaNgKpa0j-e46xilhvVzx1r8x-7,CsCyC4C.DFDMDTDbDiDqDyD7EEENEWEgEqE0E.FKFWFiFuF7GIGWGkGzHCHSHiHzIFIXIqI9JRJmJ7KSKpLALZLzMN&chtt=S+%26+P+500%7C1962+-+2008&chts=FFFFFF,14&chg=100.0,6.78,5,0&chxt=y,x,x,x&chxr=0,0.0,1475.25|1,0.0,100.0|2,1962.0,2008.0|3,0.0,100.0&chxs=0,FFFFFF,12,0|1,FFFFFF,12,0|2,FFFFFF,12,0|3,FFFFFF,14,0&chxl=1:|Fed+Chiefs%3A|Burns|Volcker|Greenspan|Bernanke|3:|Year&chxp=1,5,18,39,55,92|3,50.0&chdl=S+%26+P+500|Inflation&chco=FFFF00,32CD32&chm=o,FFFF00,0,-1,10,0|o,000000,0,-1,7,0|v,0000FF,0,8,3,0|v,0000FF,0,17,3,0|v,0000FF,0,24,3,0|v,0000FF,0,40,3,0|B,FFFFE0,0,0,0|o,00FF00,1,-1,10,0|o,000000,1,-1,7,0|B,90EE90,1,0,0&chls=3,1,0|3,1,0&cht=lc";
151  
        assertEquals("Junit error", normalize(expectedString), normalize(url));
152  
    }
153  
154  
}

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001956
Snippet name: LineChartExample (charts4j demo)
Eternal ID of this version: #1001956/1
Text MD5: 1b481c40c85ba7c9f5d6d0b8b0a9e1d2
Author: stefan
Category:
Type: Java source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-09 19:31:39
Source code size: 8773 bytes / 154 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 903 / 181
Referenced in: [show references]