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