DreamSea530

如下图 要实现该体温折线图,原本打算使用Jfreechart实现,苦苦研究几天后没有结果,发现Jfreechart貌似不能实现此复杂的功能,因为我们要实现的列头是两个,而且有重复的columnKey,两点间差值大于1.5用”√”号标记。。。既然没有找到合适的方法实现,于是只好自己编写Graphics类手工绘制,功能基本上满足要求,香港服务器,香港空间,就是字体、线条、外观感觉还是差了点,服务器空间,大家有什么好的建议还望不吝提出O(∩_∩)O~

实现过程

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

    /*** column line[] times = new int[] { 3, 7, 11 };int loopTimes = 0;for (int i = 0; i <= CELL_COUNT; i++, loopTimes++) {if (i != CELL_COUNT) {(loopTimes == 3)loopTimes = 0;graphics.setColor(Color.BLACK);graphics.drawString(times[loopTimes] + “”, (int) (MARGIN_LEFT + cellWidth * i + 2.5), cellHeight + 13);}(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++) {(0 == i % 5) {// 温度刻度graphics.setColor(Color.BLACK);graphics.drawString(–temp + “”, MARGIN_LEFT – cellWidth, cellHeight * i + 5);if (37 == temp)graphics.setColor(Color.RED);elsegraphics.setColor(new Color(180, 180, 180));// 分割线} elsegraphics.setColor(new Color(201, 221, 221));(2 == i)graphics.draw(new Line2D.Float(0, cellHeight * i, MARGIN_LEFT + cellWidth * CELL_COUNT, cellHeight * i));elsegraphics.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; (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);(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++;}}

大把大把的时光从指缝间遛走,

DreamSea530

相关文章:

你感兴趣的文章:

标签云: