2014-06-23 1 views
0

사용자 정수 순위를 보여주는 막대 차트를 만들려고합니다. 수직축은 총 사용자 수에서 차지하는 순위입니다. 나는 겹쳐 쌓인 막 대형 차트가 올바른 종류라고 생각하지만, # 1이 막대의 맨 위에있을 것이므로 #last가 맨 아래에있을 것이기 때문에 나는 그것을 수행하는 방법에 대해 머리를 감쌀 수 없습니다. 예를 들어 사용자가 100 명 중 25 명인 경우 표시 줄은 4 분의 1에 해당하는 색 하나가 표시되고 다른 하나는 그 위에있는 다른 모든 사용자를 나타내는 색입니다. # 1 사용자의 막대는 모두 하나의 색상이되고 # 가장 가까운 사용자는 다른 색상이됩니다.JFreeChart의 랭킹 차트

내 주요 문제는 내가 막대의 높이를 결정하는 알고리즘을 알아낼 수 없다는 것입니다.

도움 주셔서 감사합니다.

편집 : 관심있는 모든 사람들을 위해이 문제를 해결하기 위해 그래프 작업을 직접 작성했습니다.

package test2; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class Test2 { 

public static void main(String[] args) { 
    int rank=75; 
    int totalUsers=100; 
    int barWidth=400; 
    int barHeight=717; 
    double intervalTotal = (double)totalUsers-1.0; 
    double intervalPixelSize = (double)barHeight/intervalTotal;   
    int aboveRankBarHeight=(int)Math.round(intervalPixelSize*((double)rank-1.0)); 
    int belowRankBarHeight=barHeight-aboveRankBarHeight; 
    if (rank==1){aboveRankBarHeight=0;} 
    if (rank==totalUsers){belowRankBarHeight=0;} 

    //Axis and chart labels. 
    String topRank = "#1"; 
    String myRank = "Your Rank #"+Integer.toString(rank); 
    String bottomRank = "#"+Integer.toString(totalUsers); 

    Color imageBackground=Color.white; 
    Color axisLines=new Color(128,128,128); //Medium Gray axis lines. 
    Color graphOutline=new Color(96,96,96); //Darker Gray graph outline. 
    Color graphBackground=new Color(192,192,192); //Lighter Gray graph background. 
    Color belowRank=new Color(85,85,255); //Blue for bar below user rank. 
    Color aboveRank=new Color(255,85,85); //Red for bar above user rank. 
    Color rankLine=Color.black; 

    BufferedImage image = new BufferedImage(1200, 900, BufferedImage.TYPE_INT_RGB); 
    Graphics g = image.getGraphics(); 
    g.setColor(imageBackground); //White image background. 
    g.fillRect(0,0,1200,900);  //Draw the white. 
    g.setColor(axisLines);   //Medium Gray axis lines. 
    g.drawLine(46, 35, 46, 788); //Draw y-axis. 
    g.drawLine(50, 792, 1189, 792); //Draw x-axis. 
    g.setColor(graphOutline);  //Darker Gray graph outline. 
    g.drawRect(50, 35, 1139, 753); //Draw graph outline. 
    g.setColor(graphBackground); //Lighter Gray graph background. 
    g.fillRect(51,36,1138,752);  //Draw the graph background. 
    g.setColor(aboveRank);   //Red bar for above rank. 
    g.fillRect(419,70,barWidth,aboveRankBarHeight);  //Draw the above rank bar. 
    g.setColor(belowRank);   //Blue bar for below rank. 
    g.fillRect(419,70+aboveRankBarHeight,barWidth,belowRankBarHeight);  //Draw the below rank bar. 
    g.setColor(rankLine);   //Black line for rank. 
    g.fillRect(419,70+aboveRankBarHeight-5,barWidth,10); //Draw the rank line 10 pixels wide, 5 on either side of the border. 
    g.setFont(new Font(null, Font.PLAIN, 10)); //Plain 10-point default font for axis ticks. 
    g.drawString(topRank, 30, 70); //Draw top rank label. 
    g.drawString(bottomRank, 15, 788); //Draw bottom rank label. 
    g.setFont(new Font(null, Font.BOLD, 20)); //Bold 20-point default font for user rank label. 
    g.drawString(myRank, 250, 70+aboveRankBarHeight); //Draw user rank label. 
    g.setFont(new Font(null, Font.BOLD, 20)); //Bold 20-point default font for graph title. 
    g.drawString("Rank",575,30); //Draw graph title. 
    g.setFont(new Font(null, Font.BOLD, 20)); //Bold 20-point default font for axis labels. 
    g.drawString("Contributors",565,815); //Draw x-axis label. 

    //Draw vertical y-axis label. 
    Graphics2D g2= (Graphics2D)g; 
    AffineTransform fontAT = new AffineTransform(); //Create a rotation transformation for the font. 
    Font theFont = g2.getFont();     //Get the current font. 
    fontAT.rotate(270 * Math.PI/180);    //Derive a new font using a rotatation transform. 
    Font theDerivedFont = theFont.deriveFont(fontAT); //Apply the transform to a new font. 
    g2.setFont(theDerivedFont);      //Set the derived font in the Graphics2D context. 
    g2.drawString("Number of Edits Made", 25, 488); //Draw y-axis label. 
    g2.setFont(theFont); //Put the original font back. 

    try { 
     ImageIO.write(image, "png", new File("CustomImage.png")); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 
+0

가 [mcve] (http://stackoverflow.com/help/mcve)는 그 접근 방식을 보여줍니다 포함하도록 질문을 수정하십시오; 완전한 예제가 인용 [here] (http://stackoverflow.com/a/2782702/230513). – trashgod

답변

1

단지 각 사용자에 대해 열 및 두 행 (사용자의 수를 포함하는 하나가 더 높은 순위 및 사용자의 수를 포함하는, 다른 하나는 그 열에있는 사용자보다 낮은 기록) 데이터 집합을 만든다. 그럼 그냥 예를 들어, 누적 막대 그래프를 만들 수 있습니다

public class BarChartDemo2 extends JFrame { 

    public BarChartDemo2(String title) { 
     super(title); 
     CategoryDataset dataset = createDataset(); 
     JFreeChart chart = createChart(dataset); 
     ChartPanel chartPanel = new ChartPanel(chart); 
     chartPanel.setPreferredSize(new Dimension(500, 270)); 
     setContentPane(chartPanel); 
    } 

    private static CategoryDataset createDataset() { 
     DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
     dataset.addValue(100, "Users with lower rank", "Mary"); 
     dataset.addValue(0, "Users With higher rank", "Mary"); 
     dataset.addValue(70, "Users with lower rank", "John"); 
     dataset.addValue(30, "Users With higher rank", "John"); 
     dataset.addValue(0, "Users with lower rank", "Bill"); 
     dataset.addValue(100, "Users With higher rank", "Bill"); 
     return dataset; 
    } 

    private static JFreeChart createChart(CategoryDataset dataset) { 
     JFreeChart chart = ChartFactory.createStackedBarChart("User Ranking", 
       "User", "Rank", dataset); 
     chart.setBackgroundPaint(Color.white); 
     CategoryPlot plot = (CategoryPlot) chart.getPlot(); 
     NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 
     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
     BarRenderer renderer = (BarRenderer) plot.getRenderer(); 
     renderer.setBarPainter(new StandardBarPainter()); 
     renderer.setSeriesPaint(0, new Color(50, 240, 50)); 
     renderer.setSeriesPaint(1, new Color(240, 50, 50)); 
     renderer.setDrawBarOutline(false); 
     return chart; 
    } 

    public static void main(String[] args) { 
     BarChartDemo2 demo = new BarChartDemo2("JFreeChart: Stack Overflow Question"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 

}