import React ,{ useEffect } from 'react';
import * as echarts from 'echarts';
import 'echarts/lib/chart/line';
import axios from 'axios';
import { getdata } from './data';

function Line({url}) {
 
  useEffect(()=>{
    if(url){
      getInit(url);
    }
  },[url])

  function getInit(url){
    axios.get(url).then(result=>{
      if(result && result.data){
        Init(result.data);
      }
    }).catch(error=>{})
  }

  function Init(data) {
    var chartDom = document.getElementById('line');
    var myChart = echarts.init(chartDom);
    var option;
    myChart.setOption(
      (option = {
        tooltip: {
          trigger: 'axis'
        },
        color: ['red'],
        grid: {
          left: '5%',
          right: '1%',
          top: '8%'
        },
        xAxis: {
          data: data.map(function (item) {
            return item[0];
          })
        },
        yAxis: {
          type: 'value',
          scale: true,
          name: '代\n码\n变\n更\n演\n化\n拓\n扑\n熵',
          min:0,
          splitLine : {
            show : true,
          },
          nameLocation:'end',
          nameTextStyle:{
            fontSize:14,  
            padding:[0,70,-120,0]
          }
        },
        dataZoom: [
          {
            type: 'inside',
            start: 0,
            end: 50
          },
          {
            start: 0,
            end: 50
          }
        ],
        series: {
          type: 'line',
          data: data.map(function (item) {
            return item[1];
          })
        }
      })
    );
    option && myChart.setOption(option);
  }

  return(
    <div id="line" style={{height:"400px"}}></div>
  )
}
export default Line;