Construction of Java round chart



  • I'm trying to build a circular diagram based on a mass that will reflect the element, and depending on how many times it will be repeated. I've done everything in the classics: I've created a map, I've got an element of mass and a number of repetitions. I was dealing with the problem of withdrawal (see figure). The problem is that cluster 1 is more frequent, but in the diagram it corresponds to a smaller area than, for example, 5, which occurs only once;

    public class CreateChart extends JFrame{
    
    public CreateChart(String appTitle, String chartTitle)
    {
        PieDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset,chartTitle);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500,300));
        setContentPane(chartPanel);
    }
    
    
    private PieDataset createDataset()
    { int[] mas = {1,1,1,2,2,4,3,2,3,4,5};
      Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    
     for(int i=0;i<mas.length;++i)
     {
         if(map.containsKey(mas[i]))
         {
             map.put(mas[i], map.get(mas[i])+1);
         }
         else {map.put(mas[i], 1);}
     }
    
      DefaultPieDataset result = new DefaultPieDataset();
    
      for (Map.Entry entry : map.entrySet()) 
      {
          result.setValue(entry.getKey().toString(),(int)entry.getValue()); 
      }
    }
    private JFreeChart createChart(PieDataset dataset, String title)
    {
        JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true, true, false);
    
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(90);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
    }
    
    public static void main(String[] args) 
    {
            CreateChart CC = new CreateChart("Pie Chart Test","OS Comparison");
            CC.pack();
            CC.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            CC.setVisible(true);
    }
    

    }

    I forgot to add a drawing. 1 occurs more frequently but in the diagram the area is smaller

    введите сюда описание изображения

    Updating

    I noticed a mistake in rezult.setValue(). That's right.

    for (Map.Entry entry : map.entrySet())
    {
    result.setValue(entry.getValue().toString(),(int)entry.getKey());
    }

    But now the diagram does not display all the elements (nos 4 and 5).

    введите сюда описание изображения



  • I checked it out. result.setValue Accepts as a second argument the type doubleso it's more reasonable to use a type-to-use double(if Java 7 or above) or Double

    for (Map.Entry entry : map.entrySet()) {
         result.setValue(entry.getKey().toString(), (Double)entry.getValue());
    }
    

    Скрин


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2