如何从折线图更改为条形图

时间:2019-01-24 23:34:21

标签: java charts jfreechart

我有此折线图,并希望使其成为条形图,但是我是Java的新手,并且不知道是否可行,因为x轴为TimeSeries。这是我看到的可视化折线图的代码:

public class Time {

private static Time INSTANCE;
public static boolean isInitialized = false;

private Marker marker;
private Long markerStart;
private Long markerEnd;

private XYPlot plot;

long last_lowerBound;
long last_upperBound;

@Inject
public Time() {

}

Composite comp;
TimeSeriesCollection dataset;
ChartPanel panel;
JFreeChart chart;
protected Point endPoint;

@PostConstruct
public void postConstruct(Composite parent) {
    comp = new Composite(parent, SWT.NONE | SWT.EMBEDDED);
    Frame frame = SWT_AWT.new_Frame(comp);

    JApplet rootContainer = new JApplet();

    TimeSeries series = new TimeSeries("Timeline");
    dataset = new TimeSeriesCollection();

    String plotTitle = "";
    String xaxis = "Time";
    String yaxis = "Docs";
    PlotOrientation orientation = PlotOrientation.VERTICAL;
    boolean show = false;
    boolean toolTips = true;
    boolean urls = false;

    chart = ChartFactory.createTimeSeriesChart(plotTitle, xaxis, yaxis, dataset, show, toolTips, urls );

    // get a reference to the plot for further customisation...
    plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.gray);
    plot.setRangeGridlinePaint(Color.gray);
    plot.setOutlinePaint(Color.white);
    plot.getRangeAxis().setLabel("");
    plot.getDomainAxis().setLabel("");
    ValueAxis y_axis = plot.getRangeAxis();     // Y
    ValueAxis x_axis = plot.getDomainAxis();    // X
    Font font = new Font("Veranda", Font.PLAIN, 12);
    y_axis.setTickLabelFont(font);
    x_axis.setTickLabelFont(font);
    x_axis.setTickLabelPaint(Color.black);
    y_axis.setTickLabelPaint(Color.black);
    plot.getDomainAxis().setAxisLineVisible(false);

    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    // renderer.setSeriesLinesVisible(0, false);
    renderer.setSeriesShapesVisible(0, false);
    plot.setRenderer(renderer);

我应该只更新以下行:chart = ChartFactory.createTimeSeriesChart(plotTitle, xaxis, yaxis, dataset, show, toolTips, urls );还是应该完全更改它?

我试图像这样改变这部分,但是它什么也没显示:

dataset = new TimeSeriesCollection();

    String plotTitle = "";
    String xaxis = "Time";
    String yaxis = "Docs";
    PlotOrientation orientation = PlotOrientation.VERTICAL;
    boolean show = false;
    boolean toolTips = true;
    boolean urls = false;

    chart = ChartFactory.createBarChart(plotTitle, xaxis, yaxis, (CategoryDataset) dataset, orientation, show, toolTips, urls);


    chart.setBackgroundPaint(null);
    chart.setBackgroundImageAlpha(0.0f);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setRangeGridlinesVisible(false);
    plot.setDomainGridlinesVisible(false);
    plot.setOutlineVisible(false);
    plot.setRangeZeroBaselineVisible(false);
    plot.setDomainGridlinesVisible(false);
    plot.setBackgroundPaint(COLOR_INVISIBLE);
    plot.setBackgroundImageAlpha(0.0f);

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setSeriesPaint(0, AttributeGuiTools.getColorForValueType(Ontology.NOMINAL));
    renderer.setBarPainter(new StandardBarPainter());
    renderer.setDrawBarOutline(true);
    renderer.setShadowVisible(false); } } 

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

给定合适的XYDataset,例如IntervalXYDataset,您可以将ChartFactory.createTimeSeriesChart()替换为ChartFactory.createXYBarChart(),以提供可选的 private: void SetDefaultFanSpeed(int iAdapter, int iTController) { //Fan Reset ADLODNFanControl odNFanControl; memset(&odNFanControl, 0, sizeof(ADLODNFanControl)); if (ADL_OK != ADL2_OverdriveN_FanControl_Get(context, lpAdapterInfo[iAdapter].iAdapterIndex, &odNFanControl)) { fprintf(stderr, "Fail 1/2\n"); } else { odNFanControl.iMode = ADLODNControlType::ODNControlType_Auto; if (ADL_OK != ADL2_OverdriveN_FanControl_Set(context, lpAdapterInfo[iAdapter].iAdapterIndex, &odNFanControl)) { fprintf(stderr, "Fail 2/2\n"); } else { fprintf(stderr, "Success: 1\n"); } } if (ADL_OK != ADL_Overdrive5_FanSpeedToDefault_Set(iAdapter, iTController)) { fprintf(stderr, "Error: cannot set Fan Speed to default.\n"); } else { fprintf(stderr, "Success: 2\n"); } if (ADL_OK != ADL_Overdrive6_FanSpeed_Reset(iAdapter)) { fprintf(stderr, "Error: cannot set Fan Speed to default.\n"); } else { fprintf(stderr, "Success: 3\n"); } if (ADL_OK != ADL2_Overdrive6_FanSpeed_Reset(context,iAdapter)) { fprintf(stderr, "Error: cannot set Fan Speed to default.\n"); } else { fprintf(stderr, "Success: 4\n"); } if (ADL_OK != ADL2_Overdrive5_FanSpeedToDefault_Set(context, iAdapter, iTController)) { fprintf(stderr, "Error: cannot set Fan Speed to default.\n"); } else { fprintf(stderr, "Success: 5\n"); } } 。下面的示例使用相同的数据集来创建两个图表。

DateAxis

image

IntervalXYDataset dataset = createDataset();
f.add(createPanel(ChartFactory.createXYBarChart("Data", "Time", true, "Value", dataset)));
f.add(createPanel(ChartFactory.createTimeSeriesChart("Data", "Time", "Value", dataset)));