Java繪圖——體溫折線圖


    如下圖 要實現該體溫折線圖,原本打算使用Jfreechart實現,苦苦研究幾天后沒有結果,發現Jfreechart貌似不能實現此復雜的功能,因為我們要實現的列頭是兩個,而且有重復的columnKey,兩點間差值大於1.5用"√"號標記。。。既然沒有找到合適的方法實現,於是只好自己編寫Graphics類手工繪制,功能基本上滿足要求,就是字體、線條、外觀感覺還是差了點,大家有什么好的建議還望不吝提出O(∩_∩)O~

實現過程

Ø使用BufferedImage創建Graphics2D對象,然后生成6*7的列(column),7*5的行(row)

    /**
         * column line
         */
        // 時間
        int[] times = new int[] { 3, 7, 11 };
        int loopTimes = 0;
        for (int i = 0; i <= CELL_COUNT; i++, loopTimes++) {
            if (i != CELL_COUNT) {
                // 列頭顯示時間
                if (loopTimes == 3)
                    loopTimes = 0;
                graphics.setColor(Color.BLACK);
                graphics.drawString(times[loopTimes] + "", (int) (MARGIN_LEFT + cellWidth * i + 2.5), cellHeight + 13);
            }
            // 每隔一天用分隔符分割
            if (0 == i % 6) {
                graphics.setStroke(separator);
                graphics.setColor(new Color(162, 115, 73));
                graphics.drawLine(MARGIN_LEFT + cellWidth * i, 0, MARGIN_LEFT + cellWidth * i, height);
            } else {
                graphics.setStroke(basicStroke);
                graphics.setColor(new Color(221, 221, 221));
                graphics.drawLine(MARGIN_LEFT + cellWidth * i, cellHeight, MARGIN_LEFT + cellWidth * i, height);
            }
        }

        /*
         * row line
         */
        int temp = TEMPERATURE;
        graphics.setStroke(basicStroke);
        for (int i = 1; i <= 40; i++) {
            // 每隔1°使用分隔符分割
            if (0 == i % 5) {
                // 溫度刻度
                graphics.setColor(Color.BLACK);
                graphics.drawString(--temp + "", MARGIN_LEFT - cellWidth, cellHeight * i + 5);
                if (37 == temp)
                    graphics.setColor(Color.RED);
                else
                    graphics.setColor(new Color(180, 180, 180));// 分割線
            } else
                graphics.setColor(new Color(201, 221, 221));
            // 第二行的線為標題,和表格等寬
            if (2 == i)
                graphics.draw(new Line2D.Float(0, cellHeight * i, MARGIN_LEFT + cellWidth * CELL_COUNT, cellHeight * i));
            else
                graphics.draw(new Line2D.Float(MARGIN_LEFT, cellHeight * i, MARGIN_LEFT + cellWidth * CELL_COUNT, cellHeight * i));
        }

 

Ø創建數據折線

Set<Entry<String, Float[]>> set = map.entrySet();
        Iterator<Entry<String, Float[]>> iterator = set.iterator();
        int index = 0;
        graphics.setColor(Color.BLACK);
        float cellHalfWidth = cellWidth / 2f;
        // 體溫折線開始的位置
        Float startY = null, startX = null;
        float preTemp = 0; // 上一次記錄的提問數據
        while (iterator.hasNext()) {
            Entry<String, Float[]> entry = iterator.next();
            // 填充時間
            graphics.setFont(new Font("宋體", Font.PLAIN, 13));
            graphics.drawString(entry.getKey(), (int) (MARGIN_LEFT + cellWidth * (index + 1.1)), 12);
            // 折線
            Float[] values = entry.getValue();
            for (int i = 0; i < values.length; i++) {
                Float currTemp = values[i];
                Float endX = MARGIN_LEFT + cellWidth * index + cellHalfWidth;
                if (null != currTemp) {
                    //降溫度值轉換為坐標點
                    Float endY = (TEMPERATURE - currTemp) * (cellHeight * 5);
                    graphics.setColor(Color.BLACK);
                    graphics.fillOval((int) (endX - 2), (int) (float) endY - 2, 5, 5);
                    if (null != startY) {
                        Line2D.Float line = new Line2D.Float(startX, startY, endX, endY);
                        graphics.draw(line);
                        // 兩次測量值相差1.5以上,則用"√"標記
                        if (Math.abs(preTemp - currTemp) >= 1.5) {
                            graphics.setColor(Color.RED);
                            graphics.setFont(new Font("宋體", Font.PLAIN, 30));
                            graphics.drawString("√", endX - 15, endY - 15);
                        }
                    }
                    startY = endY;
                    startX = endX;
                    preTemp = currTemp;
                }
                index++;
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM