Plotting a hysteresis loop with jFreeChart
Asked Answered
W

1

1

I need to draw hysteresis loops and then calculate the area closed within the loop. I am using jFreeChart.

consider the following data:

 hyst[0]=0;
       hyst[1]=0;
       hyst[2]=0.0098;
       hyst[3]=0.0196;
       hyst[4]=0.0489;
       hyst[5]=0.0879;
       hyst[6]=0.0684;
       hyst[7]=0.0489;
       hyst[8]=0.0196;
       hyst[9]=0.0098;
       hyst[10]=0;
       hyst[11]=0;
       hyst[12]=0;
       hyst[13]=0;
       hyst[14]=0;
       hyst[15]=-0.0195;
       hyst[16]=-0.0488;
       hyst[17]=-0.0391;
       hyst[18]=-0.0195;
       hyst[19]=0;
       hyst[20]=0;

When I try :

   public void plotHysteresis()
   {
       int j=0;
       int i=0;
       XYSeries series1 = new XYSeries("Before Treatment");
      // DefaultCategoryDataset series1 = new DefaultCategoryDataset();
       for(i=0;i<6;i++)
       {    
        series1.add(j,hyst[i]);
        logTextArea.append(Integer.toString(j) +" : " +Double.toString(hyst[i])+"\n");
        j=j+5;
       }
       j=j-5; 
       for(;i<11;i++)
       {
        j=j-5;   
        series1.add(j,hyst[i]);
        logTextArea.append(Integer.toString(j) +" : " +Double.toString(hyst[i])+"\n");
       }
        for(;i<16;i++)
       {
        j=j-5;   
        series1.add(j,hyst[i]);
        logTextArea.append(Integer.toString(j) +" : " +Double.toString(hyst[i])+"\n");
       }
           for(;i<21;i++)
       {
        j=j+5;   
        series1.add(j,hyst[i]);
        logTextArea.append(Integer.toString(j) +" : " +Double.toString(hyst[i])+"\n");
       }

    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series1);

    JFreeChart chart = ChartFactory.createXYAreaChart(
"Hysteresis Plot", // chart title
"Pounds (lb)", // x axis label
"Distance (inches)", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
    chart.setBackgroundPaint(Color.white);

    ChartPanel frame = new ChartPanel(chart);
    frame.setVisible(true);
    frame.setSize(plotPanel.getWidth(),plotPanel.getHeight());
    plotPanel.add(frame);
    plotPanel.repaint();
   }

It gives me below result:

enter image description here

If I use :

 JFreeChart chart = ChartFactory.createXYLineChart(
"Hysteresis Plot", // chart title
"Pounds (lb)", // x axis label
"Distance (inches)", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);

I gives:

enter image description here

I need a hysteresis plot that looks like: enter image description here

I guess the difference is the way points are being connected. Please guide how to obtained the desired hysteresis loop with jFreeChart and then how to calculate area enclosed.

Thanks

Wouldbe answered 19/2, 2013 at 8:10 Comment(3)
You could plot two XYLine: one for the higher line and one for the lower one.Twit
@Twit Is there any other solution, instead of using two lines. I need to plot many such hysteresis plots for different readings obtained for hyst[23] and need to superimpose them on the same graph and compare the enclosed area changes..Wouldbe
Some ideas for area are mentioned here.Antibiotic
A
1

How can I change the line color as well the symbols representing the data points. I want all of them to be uniform.

It appears you've settled on JFreeChart for your view. Synthesizing a few other comments,

  • You can make the colors and shapes of your several series homogeneous by providing a DrawingSupplier, as suggested here and shown here.

  • You can combine the series into a GeneralPath and estimate the area as outlined here.

Antibiotic answered 19/2, 2013 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.