Search code examples
javajfreecharttrigonometry

Flat line instead wave


I want to plot a sine wave, but instead my application generates a flat line. When I use series with random from jchartfree example everything works fine. I also use debugger to check if values are good. Values are different than zero

public void createDataset() {
    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries series1 = new XYSeries("Object 1");

    double A = 1;                       
    double T = 1;                       
    double Fs = 10;                   
    double f = 200;                     
    int rozmiar = (int) (T*Fs);

    double[] x = new double[rozmiar];

   for (int i = 0; i < rozmiar; i++)
   {
        x[i] = A * Math.sin(2 * Math.PI * f * i / Fs);
        series1.add(i, x[i]);
   }

    dataset.addSeries(series1);

    data = dataset;
}
//...
public void createChartPanel() {
    //pWykres = new JPanel();
    //if(java.util.Arrays.asList(getComponents()).contains(pWykres)){
        //getContentPane().remove(pWykres);
    //}
    if(pWykres != null){
        pWykres.removeAll();
        pWykres.revalidate();
    }

    String chartTitle = "Objects Movement Chart";
    String xAxisLabel = "X";
    String yAxisLabel = "Y";

    JFreeChart chart = ChartFactory.createXYLineChart(chartTitle,
            xAxisLabel, yAxisLabel, dataset);

    customizeChart(chart);

    pWykres = new ChartPanel(chart);
    getContentPane().add(pWykres, BorderLayout.CENTER);
    setSize(620, 460);
    //validate();
    pWykres.repaint();
}
//endregion
//...    
//region 
private void customizeChart(JFreeChart chart) {
    XYPlot plot = chart.getXYPlot();
    XYSplineRenderer renderer;
            renderer = new XYSplineRenderer();
            renderer.setSeriesShapesVisible(0, false);

    // sets paint color for each series
    renderer.setSeriesPaint(0, Color.RED);

    // sets thickness for series (using strokes)
    renderer.setSeriesStroke(0, new BasicStroke(1.0f));

    // sets paint color for plot outlines
    //plot.setOutlinePaint(Color.BLUE);
    //plot.setOutlineStroke(new BasicStroke(2.0f));

    // sets renderer for lines
    plot.setRenderer(renderer);

    // sets plot background
    plot.setBackgroundPaint(Color.WHITE);

    // sets paint color for the grid lines
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.BLACK);

    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(Color.BLACK);
}

Solution

  • You are taking the sine of values that are always whole multiples of 2 * PI, so all of these values will be (approximately) zero, hence your graph will end up appearing flat unless it is scaled to show up these tiny values (which are just floating point errors).

    x[i] = A * Math.sin(2 * Math.PI * f * i / Fs);
    

    where A = 1 and f/Fs = 20 and i is integer

    For example:

    Math.sin(0)             // 0.0
    Math.sin(2 * Math.PI)   // -2.4492935982947064E-16  (approximately zero)
    Math.sin(4 * Math.PI)   // -4.898587196589413E-16   (approximately zero)
    

    To see the characteristic shape of a sine wave, you need to vary the input to the sin function by much smaller increments, e.g. pi/10 or less.