view client/src/fairway/Fairwayprofile.vue @ 779:8ba1be486833

client: improve search * Add display of searchResults as list-group.
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 26 Sep 2018 13:46:18 +0200
parents 4450f2ab41e0
children c98f88ac08a4
line wrap: on
line source

<template>
    <div class="fairwayprofile">
    </div>
</template>

<style lang="scss">
.fairwayprofile {
  background-color: white;
  margin-left: auto;
  margin-right: auto;
  margin-top: auto;
  margin-bottom: auto;
}
</style>

<script>
import * as d3 from "d3";

const WATER_COLOR = "#005DFF";
const GROUND_COLOR = "#4A2F06";

export default {
  name: "fairwayprofile",
  props: [
    "data",
    "width",
    "height",
    "xScale",
    "yScaleLeft",
    "yScaleRight",
    "margin"
  ],
  data() {
    return {};
  },
  watch: {
    width() {
      this.drawDiagram();
    },
    height() {
      this.drawDiagram();
    }
  },
  methods: {
    drawDiagram() {
      const chartDiv = document.querySelector(".fairwayprofile");
      d3.select("svg").remove();
      let svg = d3.select(chartDiv).append("svg");
      svg.attr("width", this.width);
      svg.attr("height", this.height);
      const width = this.width - this.margin.right - this.margin.left;
      const height = this.height - this.margin.top - this.margin.bottom;
      const currentData = this.data;
      const { xScale, yScaleRight, graph } = this.generateCoordinates(
        svg,
        height,
        width
      );
      this.drawWaterlevel({
        graph,
        xScale,
        yScaleRight,
        height,
        width
      });
      this.drawProfile({
        graph,
        xScale,
        yScaleRight,
        currentData,
        height,
        width
      });
    },
    generateCoordinates(svg, height, width) {
      let xScale = d3
        .scaleLinear()
        .domain(this.xScale)
        .rangeRound([0, width]);

      xScale.ticks(5);
      let yScaleLeft = d3
        .scaleLinear()
        .domain(this.yScaleLeft)
        .rangeRound([height, 0]);

      let yScaleRight = d3
        .scaleLinear()
        .domain(this.yScaleRight)
        .rangeRound([height, 0]);

      let xAxis = d3.axisBottom(xScale);
      let yAxis = d3.axisLeft(yScaleLeft);
      let yAxis2 = d3.axisRight(yScaleRight);
      let graph = svg
        .append("g")
        .attr(
          "transform",
          "translate(" + this.margin.left + "," + this.margin.top + ")"
        );
      graph
        .append("g")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis.ticks(5));
      graph.append("g").call(yAxis);
      graph
        .append("g")
        .attr("transform", "translate(" + width + ",0)")
        .call(yAxis2);
      return { xScale, yScaleLeft, yScaleRight, graph };
    },
    drawWaterlevel({ graph, xScale, yScaleRight, height }) {
      let waterArea = d3
        .area()
        .x(function(d) {
          return xScale(d.x);
        })
        .y0(height)
        .y1(function(d) {
          return yScaleRight(d.y);
        });
      graph
        .append("path")
        .datum([{ x: 0, y: 0 }, { x: 300, y: 0 }])
        .attr("fill", WATER_COLOR)
        .attr("stroke", WATER_COLOR)
        .attr("d", waterArea);
    },
    drawProfile({ graph, xScale, yScaleRight, currentData, height }) {
      let profileLine = d3
        .line()
        .x(d => {
          return xScale(d.x);
        })
        .y(d => {
          return yScaleRight(d.y);
        });
      let profileArea = d3
        .area()
        .x(function(d) {
          return xScale(d.x);
        })
        .y0(height)
        .y1(function(d) {
          return yScaleRight(d.y);
        });
      graph
        .append("path")
        .datum(currentData)
        .attr("fill", GROUND_COLOR)
        .attr("stroke", GROUND_COLOR)
        .attr("stroke-width", 3)
        .attr("d", profileArea);
      graph
        .append("path")
        .datum(currentData)
        .attr("fill", "none")
        .attr("stroke", "black")
        .attr("stroke-linejoin", "round")
        .attr("stroke-linecap", "round")
        .attr("stroke-width", 3)
        .attr("d", profileLine);
    }
  },
  mounted() {
    this.drawDiagram();
  }
};
</script>