Log Scale axis values not vissible

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
plunavat
Posts: 29
Joined: Sun Jan 01, 2012 10:59 am
antibot: No, of course not.

Log Scale axis values not vissible

Post by plunavat » Sat Jul 12, 2014 9:53 am

Hello,

I am plotting a scatter graph on Logarithmic scale. I have two issues,
1. how to display axis values of all grid lines (say 10, 20, 30, 40, 50, 60, 70, 80,90,100, etc). In the image below only only 10, 20, 30 are shown on x-axis and only 100 is shown on y-axis. values are not shown in spite of space available. how do we show all values
2. there is a vertical line drawn automatically from first point to x-axis. why this is so and how to remove the same

please find my code

Code: Select all

import java.awt.BasicStroke;
import java.awt.Color;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYAreaRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class TombStone
{
    public static void main(String[] args)
    {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series1 = new XYSeries("Series1", false);
        XYSeries series2 = new XYSeries("Series2", false);
        
        series1.add(11.05, 68.3943598346066);
        series1.add(12.748000000000001, 67.95148610202949);
        series1.add(14.446000000000002, 67.24794119191404);
        series1.add(16.144000000000002, 66.17590816044844);
        series1.add(17.842000000000002, 64.62757006382091);
        series1.add(19.540000000000003, 62.49510995821963);
        series1.add(21.238000000000003, 59.67071089983283);
        series1.add(22.936000000000003, 56.04655594484873);
        series1.add(24.634000000000004, 51.51482814945548);
        series1.add(26.332000000000004, 45.96771056984136);
        series1.add(22.709000000000007, 28.465098606756115);
        series1.add(21.188000000000006, 31.773494803834645);
        series1.add(19.667000000000005, 34.42410482299617);
        series1.add(18.146000000000004, 36.492438103906416);
        series1.add(16.625000000000004, 38.05400408623117);
        series1.add(15.104000000000003, 39.18431220963617);
        series1.add(13.583000000000002, 39.95887191378719);
        series1.add(12.062000000000001, 40.45319263834999);
        series1.add(10.541, 40.742783822990326);
        series1.add(9.02, 40.90315490737396);
        series1.add(11.05, 68.3943598346066);
       
        series2.add(100.77, 112.54828229576532);
        series2.add(123.713, 111.69715738364643);
        series2.add(146.656, 110.5461787622396);
        series2.add(169.59900000000002, 109.02365342951693);
        series2.add(192.54200000000003, 107.05788838345033);
        series2.add(215.48500000000004, 104.57719062201186);
        series2.add(238.42800000000005, 101.50986714317352);
        series2.add(261.37100000000004, 97.78422494490731);
        series2.add(284.314, 93.32857102518527);
        series2.add(307.257, 88.0712123819794);
        series2.add(330.2, 81.94045601326167);
        series2.add(282.41999999999996, 53.142467758918784);
        series2.add(263.23099999999994, 56.69400203886005);
        series2.add(244.04199999999994, 59.79428737361263);
        series2.add(224.85299999999995, 62.47911018230234);
        series2.add(205.66399999999996, 64.78425688405494);
        series2.add(186.47499999999997, 66.74551389799623);
        series2.add(167.28599999999997, 68.39866764325201);
        series2.add(148.09699999999998, 69.77950453894806);
        series2.add(128.908, 70.92381100421018);
        series2.add(109.719, 71.86737345816412);
        series2.add(90.53, 72.64597831993572);
        series2.add(100.77, 112.54828229576532);
        
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        
        JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y", dataset, PlotOrientation.VERTICAL, false, true, false);

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setForegroundAlpha(0.5f);
        plot.setBackgroundPaint(new Color(255, 255, 255, 255));

        plot.setDomainGridlinePaint(new Color(128, 128, 128));
        plot.setDomainGridlineStroke(new BasicStroke(1.2f));

        plot.setDomainMinorGridlinesVisible(true);
        plot.setDomainMinorGridlineStroke(new BasicStroke(0.0f));
        plot.setDomainMinorGridlinePaint(Color.BLACK);

        plot.setRangeGridlinePaint(new Color(128, 128, 128));
        plot.setRangeGridlineStroke(new BasicStroke(1.2f));
        plot.setRangeGridlinesVisible(true);
        plot.setRangeMinorGridlinesVisible(true);
        plot.setRangeMinorGridlineStroke(new BasicStroke(1.0f));
        plot.setRangeMinorGridlinePaint(Color.BLACK);

        LogarithmicAxis xAxis = new LogarithmicAxis("Flow [m3/hr]");
        LogarithmicAxis yAxis = new LogarithmicAxis("Head [m]");
        
        plot.setDomainAxis(xAxis);
        plot.setRangeAxis(yAxis);
        
        XYAreaRenderer renderer = new XYAreaRenderer();
        for (int i = 0; i < dataset.getSeriesCount(); i++)
        {
            renderer.setSeriesOutlinePaint(i, Color.black);
            renderer.setSeriesOutlineStroke(i, new BasicStroke(1.2f));
            renderer.setOutline(true);
            renderer.setSeriesPaint(i, new Color(217, 217, 217, 217));
        }
        renderer.setAutoPopulateSeriesFillPaint(false);
        plot.setRenderer(renderer);
        
       
        try
        {
            File file = new File("D:\\chart.png");
            if (!file.exists())
            {
                file.createNewFile();
            }
            ChartUtilities.saveChartAsPNG(file, chart, 900, 500);
        }
        catch (Exception  e)
        {
            e.printStackTrace();
        }
    }
}
Image
Image

Any help would be appreciated

Thanks & Regards,
Pranav C Lunavat

plunavat
Posts: 29
Joined: Sun Jan 01, 2012 10:59 am
antibot: No, of course not.

Re: Log Scale axis values not vissible

Post by plunavat » Wed Jul 30, 2014 8:09 am

Hello,

can anybody please help on the above issue.

Regards,
Pranav C Lunavat

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Log Scale axis values not vissible

Post by david.gilbert » Wed Jul 30, 2014 9:57 am

I would try LogAxis rather than LogarithmicAxis, usually it gives better results. For the data plot, I don't think you'll get what you are looking for using the XYAreaRenderer. I changed your code to use an XYLineAndShapeRenderer and get better results, although if you want to fill the regions you'll probably need to use an XYShapeAnnotation or create your own custom renderer.

Code: Select all

import java.awt.BasicStroke;
import java.awt.Color;
import java.io.File;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Tombstone
{
    public static void main(String[] args)
    {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series1 = new XYSeries("Series1", false);
        XYSeries series2 = new XYSeries("Series2", false);
        
        series1.add(11.05, 68.3943598346066);
        series1.add(12.748000000000001, 67.95148610202949);
        series1.add(14.446000000000002, 67.24794119191404);
        series1.add(16.144000000000002, 66.17590816044844);
        series1.add(17.842000000000002, 64.62757006382091);
        series1.add(19.540000000000003, 62.49510995821963);
        series1.add(21.238000000000003, 59.67071089983283);
        series1.add(22.936000000000003, 56.04655594484873);
        series1.add(24.634000000000004, 51.51482814945548);
        series1.add(26.332000000000004, 45.96771056984136);
        series1.add(22.709000000000007, 28.465098606756115);
        series1.add(21.188000000000006, 31.773494803834645);
        series1.add(19.667000000000005, 34.42410482299617);
        series1.add(18.146000000000004, 36.492438103906416);
        series1.add(16.625000000000004, 38.05400408623117);
        series1.add(15.104000000000003, 39.18431220963617);
        series1.add(13.583000000000002, 39.95887191378719);
        series1.add(12.062000000000001, 40.45319263834999);
        series1.add(10.541, 40.742783822990326);
        series1.add(9.02, 40.90315490737396);
        series1.add(11.05, 68.3943598346066);
       
        series2.add(100.77, 112.54828229576532);
        series2.add(123.713, 111.69715738364643);
        series2.add(146.656, 110.5461787622396);
        series2.add(169.59900000000002, 109.02365342951693);
        series2.add(192.54200000000003, 107.05788838345033);
        series2.add(215.48500000000004, 104.57719062201186);
        series2.add(238.42800000000005, 101.50986714317352);
        series2.add(261.37100000000004, 97.78422494490731);
        series2.add(284.314, 93.32857102518527);
        series2.add(307.257, 88.0712123819794);
        series2.add(330.2, 81.94045601326167);
        series2.add(282.41999999999996, 53.142467758918784);
        series2.add(263.23099999999994, 56.69400203886005);
        series2.add(244.04199999999994, 59.79428737361263);
        series2.add(224.85299999999995, 62.47911018230234);
        series2.add(205.66399999999996, 64.78425688405494);
        series2.add(186.47499999999997, 66.74551389799623);
        series2.add(167.28599999999997, 68.39866764325201);
        series2.add(148.09699999999998, 69.77950453894806);
        series2.add(128.908, 70.92381100421018);
        series2.add(109.719, 71.86737345816412);
        series2.add(90.53, 72.64597831993572);
        series2.add(100.77, 112.54828229576532);
        
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        
        JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y", dataset, PlotOrientation.VERTICAL, false, true, false);

        XYPlot plot = (XYPlot) chart.getPlot();
        //plot.setForegroundAlpha(0.5f);
        plot.setBackgroundPaint(new Color(255, 255, 255, 255));

        plot.setDomainGridlinePaint(new Color(128, 128, 128));
        plot.setDomainGridlineStroke(new BasicStroke(1.2f));

        plot.setDomainMinorGridlinesVisible(true);
        plot.setDomainMinorGridlineStroke(new BasicStroke(0.0f));
        plot.setDomainMinorGridlinePaint(Color.BLACK);

        plot.setRangeGridlinePaint(new Color(128, 128, 128));
        plot.setRangeGridlineStroke(new BasicStroke(1.2f));
        plot.setRangeGridlinesVisible(true);
        //plot.setRangeMinorGridlinesVisible(true);
        //plot.setRangeMinorGridlineStroke(new BasicStroke(1.0f));
        //plot.setRangeMinorGridlinePaint(Color.BLACK);

        LogAxis xAxis = new LogAxis("Flow [m3/hr]");
        LogAxis yAxis = new LogAxis("Head [m]");
        
        plot.setDomainAxis(xAxis);
        plot.setRangeAxis(yAxis);
        
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        for (int i = 0; i < dataset.getSeriesCount(); i++)
        {
            renderer.setSeriesStroke(i, new BasicStroke(2.0f));
            renderer.setBaseShapesVisible(false);
            //renderer.setSeriesOutlinePaint(i, Color.black);
            //renderer.setSeriesOutlineStroke(i, new BasicStroke(1.2f));
            //renderer.setOutline(true);
            renderer.setSeriesPaint(i, Color.RED);
        }
        renderer.setAutoPopulateSeriesFillPaint(false);
        plot.setRenderer(renderer);
        
       
        try
        {
            File file = new File("chart.png");
            if (!file.exists())
            {
                file.createNewFile();
            }
            ChartUtilities.saveChartAsPNG(file, chart, 900, 500);
        }
        catch (Exception  e)
        {
            e.printStackTrace();
        }
    }
}
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

larchicha
Posts: 3
Joined: Mon Jun 16, 2014 3:26 pm
antibot: No, of course not.

Re: Log Scale axis values not vissible

Post by larchicha » Wed Aug 13, 2014 1:20 pm

I'll take that...

Thank you, David.

plunavat
Posts: 29
Joined: Sun Jan 01, 2012 10:59 am
antibot: No, of course not.

Re: Log Scale axis values not vissible

Post by plunavat » Wed Aug 13, 2014 1:36 pm

Thanks David for your solution.
but i see two things happening,
1. the axis becomes equidistant v/s the image attached above (where the distance is more initially and then reduces gradually)
2. instead of whole numbers in axis it shows 10^0.9 and so on.

i would require the output in the same fashion as image attached before

looking for some help

Thanks & Regards
Pranav C Lunavat

plunavat
Posts: 29
Joined: Sun Jan 01, 2012 10:59 am
antibot: No, of course not.

Re: Log Scale axis values not vissible

Post by plunavat » Fri May 08, 2015 1:35 pm

Hello,

how can we display the axis values on logarithmic axis in above image. Like on Y axis there is lot of space to show the values as 10,20, etc but still it is not showing

Thanks & Regards,
Pranav C Lunavat

Locked