美文网首页
记前端曲线展示小问题解决

记前端曲线展示小问题解决

作者: 阿武_Accat | 来源:发表于2019-01-21 01:34 被阅读0次

问题描述:
开发过程中,我们的前端使用Echarts.js开发时,碰到了一个困扰她的问题。


image.png

上面的曲线的X轴为时间轴, Y轴为数值, 问题在当这几个线返回的X轴数据不一致时, Echarts.js 在那个点会出现断线。

image.png

写了个东西,将缺失的数据补全,
即X轴数据补全,然后Y轴使用临近的两个点的均值补全;
当前后没有点时,使用最近的点的数值进行补全。

package com.loyotech.bigscreenbackend;

import com.loyotech.bigscreenbackend.dto.IceThicknessCurvesDTO;
import com.loyotech.bigscreenbackend.model.IceThicknessCurve;
import com.loyotech.bigscreenbackend.util.ObjectUtil;
import lombok.extern.java.Log;
import org.junit.Test;

import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;

/*
 * @program big-screen-backend
 * @description
 * @author Rudolph Browne
 * @create 19-1-15
 */
@Log
public class curveOfIceThicknessTest {

    @Test
    public void test01() {

        List<IceThicknessCurvesDTO> result = prepareTestData();

        log.log(Level.INFO, result.toString());

        initialFakePoint(result);

        log.log(Level.INFO, result.toString());

        evaluateFakePoint(result);

        log.log(Level.INFO, result.toString());

    }

    @Test
    public void test02() {
        IceThicknessCurve a = new IceThicknessCurve() {{
            setRecPhase("C相");
            setRecIceHeight("1");
            setRecBsId("CC2529");
            setCreateDate("2019-01-13 21:34:27");
            setRecUpdateTime("2019-01-13 04:10:26");
        }};

        IceThicknessCurve b = new IceThicknessCurve();

        ObjectUtil.copyProperties(IceThicknessCurve.class, a, IceThicknessCurve.class, b);

        log.log(Level.INFO, b.toString());
    }

    @Test
    public void test03() {
        List list = Arrays.asList(1,2,3,2,1,3);

        list = (List) list.stream().distinct().collect(Collectors.toList());

        log.log(Level.INFO, list.toString());
    }
}

    private List<IceThicknessCurvesDTO> prepareTestData() {
        return new LinkedList<IceThicknessCurvesDTO>() {{
            add(new IceThicknessCurvesDTO() {{
                setPhrase("A相");
                setList(new LinkedList<IceThicknessCurve>(){{
                    add(new IceThicknessCurve() {{
                        setRecPhase("A相");
                        setRecIceHeight("1");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 21:10:26");
                    }});
                    add(new IceThicknessCurve() {{
                        setRecPhase("A相");
                        setRecIceHeight("5");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 15:10:26");
                    }});
                    add(new IceThicknessCurve() {{
                        setRecPhase("A相");
                        setRecIceHeight("1");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 05:10:26");
                    }});
                }});
            }});

            add(new IceThicknessCurvesDTO() {{
                setPhrase("B相");
                setList(new LinkedList<IceThicknessCurve>(){{
                    add(new IceThicknessCurve() {{
                        setRecPhase("B相");
                        setRecIceHeight("1");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 13:10:26");
                    }});
                    add(new IceThicknessCurve() {{
                        setRecPhase("B相");
                        setRecIceHeight("5");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 19:10:26");
                    }});
                    add(new IceThicknessCurve() {{
                        setRecPhase("B相");
                        setRecIceHeight("1");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 01:10:26");
                    }});
                }});
            }});

            add(new IceThicknessCurvesDTO() {{
                setPhrase("B相");
                setList(new LinkedList<IceThicknessCurve>(){{
                    add(new IceThicknessCurve() {{
                        setRecPhase("C相");
                        setRecIceHeight("1");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 02:34:27");
                        setRecUpdateTime("2019-01-13 13:10:26");
                    }});
                    add(new IceThicknessCurve() {{
                        setRecPhase("C相");
                        setRecIceHeight("5");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 03:10:26");
                    }});
                    add(new IceThicknessCurve() {{
                        setRecPhase("C相");
                        setRecIceHeight("1");
                        setRecBsId("CC2529");
                        setCreateDate("2019-01-13 21:34:27");
                        setRecUpdateTime("2019-01-13 04:10:26");
                    }});
                }});
            }});
        }};
    }

    private void initialFakePoint(List<IceThicknessCurvesDTO> result) {
        List<IceThicknessCurve> iceThicknessCurves = new LinkedList<>();

        result.stream().forEach(
                iceThicknessCurvesDTO -> {
                    iceThicknessCurves.addAll(iceThicknessCurvesDTO.getList());
                }
        );

        iceThicknessCurves.stream().forEach(
                iceThicknessCurve -> {
                    for (IceThicknessCurvesDTO iceThicknessCurvesDTO : result) {
                        if (!iceThicknessCurvesDTO.getPhrase().equalsIgnoreCase(iceThicknessCurve.getRecPhase())) {
                            iceThicknessCurvesDTO.getList().add(new IceThicknessCurve() {{ //为每条曲线设置空点, 适配X时间轴
                                setRecUpdateTime(iceThicknessCurve.getRecUpdateTime());
                            }});
                        }
                    }
                }
        );
    }

    private void evaluateFakePoint(List<IceThicknessCurvesDTO> result) {
        // 每一条曲线需要为虚点附上数值
        for (IceThicknessCurvesDTO iceThicknessCurvesDTO : result) {
            List<IceThicknessCurve> list = iceThicknessCurvesDTO.getList();
            list.sort((o1, o2) -> (o1.getRecUpdateTime().compareTo(o2.getRecUpdateTime())));
            Iterator iterator = list.listIterator();
            IceThicknessCurve prevRealPoint = null;
            IceThicknessCurve nextRealPoint = null;
            IceThicknessCurve currentPoint;

            // 循环执行每个点, 作为当前点
            while (iterator.hasNext()) {
                // 拿到当前点
                currentPoint = (IceThicknessCurve) iterator.next();

                // 如果当前点为实点
                if (isRealPoint(currentPoint)){
                    prevRealPoint = currentPoint;
                    continue;
                } else { // 如果当前点为虚点

                    // 如果当前点既是虚点, 又是第一个点
                    ((ListIterator) iterator).previous();
                    boolean isFirstPoint = !((ListIterator) iterator).hasPrevious();

                    if (isFirstPoint) {

                        nextRealPoint = findNextRealPoint(iterator);

//                        需要实现不想克隆的部分, 要不然会出现覆盖时间点的情况
//                        ObjectUtil.copyProperties(IceThicknessCurve.class, nextRealPoint,
//                                IceThicknessCurve.class, currentPoint);
                        currentPoint.setRecIceHeight(nextRealPoint.getRecIceHeight());
                        currentPoint.setCreateDate(nextRealPoint.getCreateDate());
                        currentPoint.setRecBsId(nextRealPoint.getRecBsId());
                        currentPoint.setRecPhase(nextRealPoint.getRecPhase());

                        prevRealPoint = currentPoint;

                        backToCurrentPoint(iterator, currentPoint, currentPoint.getClass());

                        continue;
                    }

                    nextRealPoint = findNextRealPoint(iterator);

                    if (!isRealPoint(nextRealPoint)) {

                        // 如果最后不存在实点
//                        ObjectUtil.copyProperties(IceThicknessCurve.class, prevRealPoint,
//                                IceThicknessCurve.class, currentPoint);
                        currentPoint.setRecIceHeight(prevRealPoint.getRecIceHeight());
                        currentPoint.setCreateDate(prevRealPoint.getCreateDate());
                        currentPoint.setRecBsId(prevRealPoint.getRecBsId());
                        currentPoint.setRecPhase(prevRealPoint.getRecPhase());

                    } else {
                        // 当找到左右两个实点, 将当前虚点转为实点
                        currentPoint.setRecIceHeight(String.valueOf((Double.valueOf(prevRealPoint.getRecIceHeight())
                                + Double.valueOf(nextRealPoint.getRecIceHeight()))/2)) ;
                        currentPoint.setCreateDate(prevRealPoint.getCreateDate());
                        currentPoint.setRecBsId(prevRealPoint.getRecBsId());
                        currentPoint.setRecPhase(prevRealPoint.getRecPhase());
                    }
                    prevRealPoint = currentPoint;
                    backToCurrentPoint(iterator, currentPoint, currentPoint.getClass());
                }
            }
        }
    }

    /**
     *  指针重新回到当前点
     * @param iterator
     * @param currentPoint
     * @param currentPointType
     */
    private void backToCurrentPoint(Iterator iterator, Object currentPoint, Class currentPointType) {
        while (((ListIterator) iterator).hasPrevious()) {
            if (((ListIterator) iterator).previous().equals(currentPointType.cast(currentPoint)))
                break;
        }
    }

    /**
     * 寻找下一个实点
     * @param iterator
     * @param <T>
     * @return
     */
    private <T extends IceThicknessCurve> IceThicknessCurve findNextRealPoint(Iterator iterator) {
        T nextRealPoint = null;
        while (iterator.hasNext()) {
            nextRealPoint = (T) iterator.next();
            if ((nextRealPoint).getRecIceHeight() == null || "".equals(nextRealPoint.getRecIceHeight())) {
                continue;
            } else {
                break;
            }
        }
        return nextRealPoint;
    }

    /**
     * 判断传入点是实点还是虚点
     * @param point
     * @param <T>
     * @return
     */
    private <T extends IceThicknessCurve> boolean isRealPoint(T point) {
        return point.getRecIceHeight() != null && !"".equals(point.getRecIceHeight());
    }

相关文章

  • 记前端曲线展示小问题解决

    问题描述:开发过程中,我们的前端使用Echarts.js开发时,碰到了一个困扰她的问题。 上面的曲线的X轴为时间轴...

  • 玩转单细胞(3):堆叠柱状图添加比例

    每次解决一个小问题,久而久之,你就解决了一个大麻烦: 单细胞比例的展示,常见的是堆叠柱状图,但是有一个小问题,比较...

  • 快速排序

    快速排序 分而治之思想主要把解决一个大问题分为以下步骤解决 把大问题拆成许多小问题 分别解决小问题 把小问题答案组...

  • 前后端分离_分页

    在分页类编写数据,展示前端实现跳转 你的数据样式 前端接收数据,展示

  • Bezier曲线切割

    贝塞尔曲线 目标:在unity中展示Bezier曲线。 贝塞尔曲线原理 用代码实现 bezier数学公式 Bezi...

  • 解决小问题

    今天给老妈打电话,她的音量很小。等到见面时,想要给她调整过来(老妈不识字,玩不了智能机,只能用老年机,偶尔打个电话...

  • 前端小问题总结

    1.setTimeout无效,需将调用函数加上双引号setTimeout("flushProgramUI()",f...

  • Vue+Wowjs动画演示

    场景 前端同事使用vue开发网页,并需要使用wowjs进行动画效果展示。但wowjs并未生效,找不到原因。 解决 ...

  • Universal Link 前端部署采坑记

    Universal Link 前端部署采坑记 Universal Link 前端部署采坑记

  • 苹果树健康小问答20190207

    苹果树: 我问你一个问题啊,小问题容易解决还是大问题容易解决? 小苹果: 自然是小问题容易解决。 苹果树: 那么身...

网友评论

      本文标题:记前端曲线展示小问题解决

      本文链接:https://www.haomeiwen.com/subject/jmkqjqtx.html