List of usage examples for org.jfree.chart.axis DateTickUnitType DAY
DateTickUnitType DAY
To view the source code for org.jfree.chart.axis DateTickUnitType DAY.
Click Source Link
From source file:com.freedomotic.jfrontend.extras.GraphPanel.java
private void createChart(UsageDataFrame points, String title) { series = new TimeSeries(title); for (UsageData d : points.getData()) { Date resultdate = d.getDateTime(); Millisecond ms_read = new Millisecond(resultdate); int poweredValue = -1; if (d.getObjBehavior().equalsIgnoreCase("powered")) { poweredValue = d.getObjValue().equalsIgnoreCase("true") ? 1 : 0; } else if (d.getObjBehavior().equalsIgnoreCase("brigthness")) { try { poweredValue = Integer.parseInt(d.getObjValue()); } catch (NumberFormatException ex) { poweredValue = -1;//from w ww. j a v a2s. c o m } } series.addOrUpdate(ms_read, poweredValue); } XYDataset xyDataset = new TimeSeriesCollection(series); chart = ChartFactory.createTimeSeriesChart("Chart", "TIME", "VALUE", xyDataset, true, // legend true, // tooltips false // urls ); chart.setAntiAlias(true); // Set plot styles XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(2.0, 2.0, 2.0, 2.0)); // Set series line styles plot.setRenderer(new XYStepRenderer()); XYItemRenderer r = plot.getRenderer(); if (r instanceof XYLineAndShapeRenderer) { XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; renderer.setShapesVisible(true); renderer.setShapesFilled(true); } // Set date axis style DateAxis axis = (DateAxis) plot.getDomainAxis(); String formatString = "MM-dd HH"; DateTickUnitType dtut = DateTickUnitType.HOUR; if (jComboGranularity.getSelectedItem().equals("Year")) { formatString = "yyyy"; dtut = DateTickUnitType.YEAR; } else if (jComboGranularity.getSelectedItem().equals("Month")) { axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM")); dtut = DateTickUnitType.MONTH; } else if (jComboGranularity.getSelectedItem().equals("Day")) { axis.setDateFormatOverride(new SimpleDateFormat("MM-dd")); dtut = DateTickUnitType.DAY; } else if (jComboGranularity.getSelectedItem().equals("Minute")) { formatString = "MM-dd HH:mm"; dtut = DateTickUnitType.MINUTE; } else if (jComboGranularity.getSelectedItem().equals("Second")) { formatString = "HH:mm:SS"; dtut = DateTickUnitType.SECOND; } DateFormat formatter = new SimpleDateFormat(formatString); DateTickUnit unit = new DateTickUnit(dtut, 1, formatter); axis.setTickUnit(unit); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(800, 500)); graphPanel.removeAll(); graphPanel.add(chartPanel); }
From source file:edu.jhuapl.graphs.jfreechart.JFreeChartTimeSeriesGraphSource.java
private static DateTickUnit getDateTickUnit(TimeResolution minimumResolution, int qty) throws GraphException { DateTickUnitType dateTypeUnit;// w w w. ja v a 2 s . c om switch (minimumResolution) { case HOURLY: dateTypeUnit = DateTickUnitType.HOUR; break; case DAILY: case WEEKLY: dateTypeUnit = DateTickUnitType.DAY; break; case MONTHLY: dateTypeUnit = DateTickUnitType.MONTH; break; case YEARLY: dateTypeUnit = DateTickUnitType.YEAR; break; default: throw new GraphException("Unrecognized resolution \"" + minimumResolution + "\""); } return new DateTickUnit(dateTypeUnit, qty); }
From source file:de.fau.amos.ChartRenderer.java
/** * Creates Chart with Bars (JFreeChart object) from TimeSeriesCollection. Is used when granularity is set to days, months or years. * Adjusts display of the chart, not the values itself. * /*from ww w . j a v a 2 s .com*/ * @param collection TimeSeriesCollection that should be used as basis of chart. * @param timeGranularity Selected granularity. Value is similar to granularity contained in TimeSeriesCollection "collection". * @param time first element of time that is displayed. Is used for caption text and axis text. * @param unit Unit of displayed values (kWh or kWh/TNF). * @return Returns finished JFreeChart object. */ private JFreeChart createTimeBarChart(TimeSeriesCollection collection, String timeGranularity, String time, String unit) { String xAxisLabel = null; // Modification of X-Axis Label (depending on the granularity) int month; String monthString = null; switch (timeGranularity) { //for Case "0" see method "createTimeLineChart" case "1": month = Integer.parseInt(time.substring(5, 7)); monthString = new DateFormatSymbols(Locale.US).getMonths()[month - 1]; xAxisLabel = "" + monthString + " " + time.substring(0, 4); break; case "2": xAxisLabel = time.substring(0, 4); break; case "3": xAxisLabel = "Years"; break; default: xAxisLabel = "Timespan"; } JFreeChart barChart = ChartFactory.createXYBarChart("Bar Chart", // title xAxisLabel, // x-axis label true, // date axis? // "Energy Consumption "+("1".equals(unit)?"[kWh]":("2".equals(unit)?"[kWh/TNF]":("3".equals(unit)?"[TNF]":""))), // y-axis label ("1".equals(unit) ? "Energy Consumption [kWh]" : ("2".equals(unit) ? "Energy Consumption [kWh/TNF]" : ("3".equals(unit) ? "Produced Pieces [TNF]" : ""))), collection, // data PlotOrientation.VERTICAL, // orientation true, // create legend? true, // generate tooltips? false // generate URLs? ); //graphical modifications for BarChart barChart.setBackgroundPaint(Color.white); XYPlot plot = barChart.getXYPlot(); plot.setBackgroundPaint(Color.white); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0)); //Axis modification: Set Axis X-Value directly below the bars DateAxis dateAxis = (DateAxis) plot.getDomainAxis(); dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE); //Axis modification: Remove Values from x-Axis that belong to former/later time element (Month/Day) dateAxis.setLowerMargin(0.01); dateAxis.setUpperMargin(0.01); //Axis modification: Axis values (depending on timeGranularity) if (timeGranularity.equals("1")) { dateAxis.setTickUnit( new DateTickUnit(DateTickUnitType.DAY, 2, new SimpleDateFormat(" dd. ", Locale.US))); } if (timeGranularity.equals("2")) { dateAxis.setTickUnit( new DateTickUnit(DateTickUnitType.MONTH, 1, new SimpleDateFormat(" MMM ", Locale.US))); } if (timeGranularity.equals("3")) { dateAxis.setTickUnit( new DateTickUnit(DateTickUnitType.YEAR, 1, new SimpleDateFormat(" yyyy ", Locale.US))); } ClusteredXYBarRenderer clusteredxybarrenderer = new ClusteredXYBarRenderer(0.25, false); clusteredxybarrenderer.setShadowVisible(false); clusteredxybarrenderer.setBarPainter(new StandardXYBarPainter()); plot.setRenderer(clusteredxybarrenderer); return barChart; }
From source file:net.sf.jasperreports.chartthemes.simple.SimpleChartTheme.java
/** * *//*from w w w .j a v a 2s . co m*/ protected JFreeChart createXYBarChart() throws JRException { IntervalXYDataset tmpDataset = (IntervalXYDataset) getDataset(); boolean isDate = true; if (getChart().getDataset().getDatasetType() == JRChartDataset.XY_DATASET) { isDate = false; } ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()); JFreeChart jfreeChart = ChartFactory.createXYBarChart( evaluateTextExpression(getChart().getTitleExpression()), evaluateTextExpression(((JRBarPlot) getPlot()).getCategoryAxisLabelExpression()), isDate, evaluateTextExpression(((JRBarPlot) getPlot()).getValueAxisLabelExpression()), tmpDataset, getPlot().getOrientationValue().getOrientation(), isShowLegend(), true, false); configureChart(jfreeChart, getPlot()); XYPlot xyPlot = (XYPlot) jfreeChart.getPlot(); //plot.setNoDataMessage("No data to display"); // ((XYPlot)plot.getDomainAxis()).setTickMarksVisible( // ((JRBarPlot)getPlot()).isShowTickMarks() // ); // ((CategoryAxis)plot.getDomainAxis()).setTickLabelsVisible( // ((JRBarPlot)getPlot()).isShowTickLabels() // ); // ((NumberAxis)plot.getRangeAxis()).setTickMarksVisible( // ((JRBarPlot)getPlot()).isShowTickMarks() // ); // ((NumberAxis)plot.getRangeAxis()).setTickLabelsVisible( // ((JRBarPlot)getPlot()).isShowTickLabels() // ); XYBarRenderer itemRenderer = (XYBarRenderer) xyPlot.getRenderer(); itemRenderer.setBaseItemLabelGenerator((XYItemLabelGenerator) getLabelGenerator()); itemRenderer.setShadowVisible(false); JRBarPlot barPlot = (JRBarPlot) getPlot(); boolean isShowLabels = barPlot.getShowLabels() == null ? false : barPlot.getShowLabels(); itemRenderer.setBaseItemLabelsVisible(isShowLabels); // Handle the axis formating for the category axis configureAxis(xyPlot.getDomainAxis(), barPlot.getCategoryAxisLabelFont(), barPlot.getCategoryAxisLabelColor(), barPlot.getCategoryAxisTickLabelFont(), barPlot.getCategoryAxisTickLabelColor(), barPlot.getCategoryAxisTickLabelMask(), barPlot.getCategoryAxisVerticalTickLabels(), barPlot.getOwnCategoryAxisLineColor(), getDomainAxisSettings(), DateTickUnitType.DAY, (Comparable<?>) evaluateExpression(barPlot.getDomainAxisMinValueExpression()), (Comparable<?>) evaluateExpression(barPlot.getDomainAxisMaxValueExpression())); // Handle the axis formating for the value axis configureAxis(xyPlot.getRangeAxis(), barPlot.getValueAxisLabelFont(), barPlot.getValueAxisLabelColor(), barPlot.getValueAxisTickLabelFont(), barPlot.getValueAxisTickLabelColor(), barPlot.getValueAxisTickLabelMask(), barPlot.getValueAxisVerticalTickLabels(), barPlot.getOwnValueAxisLineColor(), getRangeAxisSettings(), DateTickUnitType.DAY, (Comparable<?>) evaluateExpression(barPlot.getRangeAxisMinValueExpression()), (Comparable<?>) evaluateExpression(barPlot.getRangeAxisMaxValueExpression())); return jfreeChart; }
From source file:net.sf.jasperreports.chartthemes.simple.SimpleChartTheme.java
protected JFreeChart createTimeSeriesChart() throws JRException { String timeAxisLabel = evaluateTextExpression(((JRTimeSeriesPlot) getPlot()).getTimeAxisLabelExpression()); String valueAxisLabel = evaluateTextExpression( ((JRTimeSeriesPlot) getPlot()).getValueAxisLabelExpression()); ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()); JFreeChart jfreeChart = ChartFactory.createTimeSeriesChart( evaluateTextExpression(getChart().getTitleExpression()), timeAxisLabel, valueAxisLabel, (TimeSeriesCollection) getDataset(), isShowLegend(), true, false); configureChart(jfreeChart, getPlot()); XYPlot xyPlot = (XYPlot) jfreeChart.getPlot(); JRTimeSeriesPlot timeSeriesPlot = (JRTimeSeriesPlot) getPlot(); XYLineAndShapeRenderer lineRenderer = (XYLineAndShapeRenderer) xyPlot.getRenderer(); boolean isShowShapes = timeSeriesPlot.getShowShapes() == null ? true : timeSeriesPlot.getShowShapes(); boolean isShowLines = timeSeriesPlot.getShowLines() == null ? true : timeSeriesPlot.getShowLines(); lineRenderer.setBaseLinesVisible(isShowLines); lineRenderer.setBaseShapesVisible(isShowShapes); // Handle the axis formating for the category axis configureAxis(xyPlot.getDomainAxis(), timeSeriesPlot.getTimeAxisLabelFont(), timeSeriesPlot.getTimeAxisLabelColor(), timeSeriesPlot.getTimeAxisTickLabelFont(), timeSeriesPlot.getTimeAxisTickLabelColor(), timeSeriesPlot.getTimeAxisTickLabelMask(), timeSeriesPlot.getTimeAxisVerticalTickLabels(), timeSeriesPlot.getOwnTimeAxisLineColor(), getDomainAxisSettings(), DateTickUnitType.DAY, (Comparable<?>) evaluateExpression(timeSeriesPlot.getDomainAxisMinValueExpression()), (Comparable<?>) evaluateExpression(timeSeriesPlot.getDomainAxisMaxValueExpression())); // Handle the axis formating for the value axis configureAxis(xyPlot.getRangeAxis(), timeSeriesPlot.getValueAxisLabelFont(), timeSeriesPlot.getValueAxisLabelColor(), timeSeriesPlot.getValueAxisTickLabelFont(), timeSeriesPlot.getValueAxisTickLabelColor(), timeSeriesPlot.getValueAxisTickLabelMask(), timeSeriesPlot.getValueAxisVerticalTickLabels(), timeSeriesPlot.getOwnValueAxisLineColor(), getRangeAxisSettings(), DateTickUnitType.DAY, (Comparable<?>) evaluateExpression(timeSeriesPlot.getRangeAxisMinValueExpression()), (Comparable<?>) evaluateExpression(timeSeriesPlot.getRangeAxisMaxValueExpression())); return jfreeChart; }
From source file:net.sf.jasperreports.chartthemes.spring.GenericChartTheme.java
/** * Returns the specific org.jfree.chart.axis.DateTickUnit time unit constant * related to the String value passed as argument * //ww w . j a v a 2s . co m * @param timePeriodUnit - a String represented by one of the following * accepted values: ["Year", "Month", "Day", "Hour", "Minute", "Second", "Millisecond"] * @return the specific org.jfree.chart.axis.DateTickUnit time unit constant */ protected DateTickUnitType getTimePeriodUnit(String timePeriodUnit) { if (timePeriodUnit == null) return DateTickUnitType.DAY; return timePeriodUnit.equals("Year") ? DateTickUnitType.YEAR : timePeriodUnit.equals("Month") ? DateTickUnitType.MONTH : timePeriodUnit.equals("Hour") ? DateTickUnitType.HOUR : timePeriodUnit.equals("Minute") ? DateTickUnitType.MINUTE : timePeriodUnit.equals("Second") ? DateTickUnitType.SECOND : timePeriodUnit.equals("Millisecond") ? DateTickUnitType.MILLISECOND : DateTickUnitType.DAY; }