Git Product home page Git Product logo

hzyhbk.github.io's People

hzyhbk.github.io's Issues

echarts-for-react 修改点击图例事件

需求:点击图例,则选中该项,将其他项置灰;再次点击,恢复选中所有选项
做法:主要是监听下legendselectchanged事件就好了。
坑点: 如果在legendselectchanged里调用 echarts 的 dispatchActions,会陷入循环调用,可参考这篇文章,我就直接调用setOption了。

import React, { PureComponent } from 'react';
import _ from 'lodash';
import styled from 'styled-components';
import ReactEcharts from 'echarts-for-react';
import moment from 'moment';

const Root = styled.div``;

type ILineChartProps = {
  className?: string;
  style?: React.CSSProperties;
  rawData: {
    data: IRawChartDataItem[];
    series: {
      name: string;
      data: number[];
      type: 'line';
      areaStyle: {};
    }[];
  };
  loading: boolean;
};
type ILineChartState = {};

class LineChart extends PureComponent<ILineChartProps, ILineChartState> {
  echartRef: any = null;
  clickedLegendName: string;

  getEchartsOption = (selected: { [k: string]: boolean } = {}) => {
    const {
      rawData: { data, series },
    } = this.props;

    const option = {
      grid: {
        top: 24,
        bottom: 8,
        left: 24,
        right: 36,
        containLabel: true,
      },
      legend: { data: series.map(item => item.name), selected },
      tooltip: {
        trigger: 'axis',
        formatter(params: any) {
          let text = '';
          if (_.isArray(params)) {
            text = `${moment(Number(params[0].axisValue)).format(
              'YYYY-MM-DD HH:mm:ss',
            )}<br/>`;
            _.forEach(params, obj => {
              let value = obj.value;
              text = `${text}${obj.marker}${obj.seriesName}: ${value}<br/>`;
            });
          } else {
            text = `${moment(Number(params.axisValue)).format(
              'YYYY-MM-DD HH:mm:ss',
            )}<br/>`;
            text = `${text}${params}${params.seriesName}: ${params.value}`;
          }
          return text;
        },
      },
      xAxis: {
        data: data.map(o => o.timestamp),
        axisLabel: {
          formatter(value: string) {
            return moment(Number(value)).format('HH:mm');
          },
        },
      },
      yAxis: {
        type: 'value',
      },
      series: series,
    };
    return option;
  };

  onLegendSelectChanged = (e: {
    name: string;
    selected: { [k: string]: boolean };
  }) => {
    let selected = Object.assign({}, e.selected);
    if (this.clickedLegendName === e.name) {
      this.clickedLegendName = '';
      Object.keys(selected).forEach(key => {
        selected[key] = true;
      });
    } else {
      this.clickedLegendName = e.name;
      Object.keys(selected).forEach(key => {
        if (key === e.name) {
          selected[key] = true;
        } else {
          selected[key] = false;
        }
      });
    }
    const option = this.getEchartsOption(selected);
    this.echartRef.setOption(option);
  };

  render() {
    const { className, style, loading } = this.props;

    return (
      <Root
        className={className}
        style={style}
      >
        <ReactEcharts
          ref={(ref:any) => (this.echartRef = ref && ref.getEchartsInstance())}
          option={this.getEchartsOption()}
          showLoading={loading}
          onEvents={{
            legendselectchanged: this.onLegendSelectChanged,
          }}
        />
      </Root>
    );
  }
}

d3如何重置 zoom

问题:拖拽或者缩放之后,重新绘制,再次触发zoom事件时,会回到上一次拖拽/缩放的状态

解决办法:

container.call(zoom)

改成

container.call(zoom.transform, d3.zoomIdentity);
container.call(zoom)

参考:d3/d3-zoom#107

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.