D3 bubble chart axis non-number type

Requirement: d3 Usually we see that either the x y axis is number or one of them is number

So how do you draw x-y axes that are non-numeric bubble charts?

To achieve the following picture:

Using the vue + d3 v4 version, the code is as follows:

Among them, the color is rendered with a scale, and the radius is too small because the original data is too small, so there is a linear scale to render~

  let self = this
          var initWidth = 800
          var initHeight = 800

          var padding = {
            left: 300,
            top: 20,
            right: 20,
            bottom: 20
          }

          var height = initWidth - padding.top - padding.bottom
          var width = initHeight - padding.left - padding.right


          var svg = d3.select(".container")
            .append("svg")
            .attr("width", width)
            .attr("height", height)
            .style("padding-left", padding.left)
            .style("padding-right", padding.right)
            .style("padding-top", padding.top)
            .style("padding-bottom", padding.bottom)

          / / Add y axis axis

                     //y axis scale
          let ydata = self.yData
          let yScale = d3.scaleBand().rangeRound([height, 0]).padding(1)
            .domain(ydata.map(function(d) {
              return d;
            }))

                     / / Define the y axis
          let yAxis = d3.axisLeft(yScale)

                     / / Add y axis
          svg.append("g")
            .attr("class", "xaxis")
            .attr("transform", "translate(" + 0 + "," + 0 + ")")
            .call(yAxis);

                     / / Add x axis axis

                     //x axis scale
          let xData = self.xData

          let xScale = d3.scaleBand().rangeRound([0, width]).padding(1)
            .domain(xData.map(function(d) {
              return d;
            }))

                     / / Define the x axis
          let xAxis = d3.axisBottom(xScale)

                     / / Add x axis
          svg.append("g")
            .attr("class", "yaxis")
            .attr("transform", "translate(" + "0 ," + height + ")")
            .call(xAxis);

          d3.selectAll('.domain').remove() // remove the extra tick marks

                     //Add grid-----------

          // gridlines in x axis function
          function make_x_gridlines() {
            return d3.axisBottom(xScale)
              .ticks(4)
          }

          // gridlines in y axis function
          function make_y_gridlines() {
            return d3.axisLeft(yScale)
              .ticks(4)
          }

          // add the X gridlines
          svg.append("g")
            .attr("class", "grid")
            .attr("transform", "translate(0," + height + ")")
            .call(make_x_gridlines()
              .tickSize(-height)
              .tickFormat("")
            )

          // add the Y gridlines
          svg.append("g")
            .attr("class", "grid")
            .call(make_y_gridlines()
              .tickSize(-width)
              .tickFormat("")
            )

        var colorLinear = d3.scaleLinear()
            .domain([d3.min(self.tableValue , function (d) {
            				  return Number(d.p_value); } ),
                    d3.max(self.tableValue , function (d) {
                    	 return Number(d.p_value); })])
            .range(["red", "blue"]);

        var radiusLinear = d3.scaleLinear()
            .domain([d3.min(self.tableValue , function (d) {
            				  return d.gene_ratio; } ),
                    d3.max(self.tableValue , function (d) {
                    	 return d.gene_ratio; })])
            .range([10, 20]);

                     / / Add circle wrap layer, there are several types to add a few
          var cover = svg.append("g")

                     / / Add circle
          cover.selectAll("circle")
            .data(self.tableValue)
            .enter()
            .append("circle")
            .attr("cx", function(d) {
              return xScale(d.x)
            })
            .attr("cy", function(d) {
              return yScale(d.y)
            })
            .attr("r", function(d) {
              return radiusLinear(d.gene_ratio)
            })
            .attr("fill", function(d) {
              return colorLinear(Number(d.p_value))
            })
            .on("mouseover", function(d) {
              let self = this;
              d3.select(this)
                .transition()
                .duration(100)
                .attr("r", d3.select(this).attr("r") * 1.6)
              showtext.attr("x", function() {
                  return xScale(d.x)
                })
                .attr("y", function() {
                  return yScale(d.y) - d3.select(self).attr("r") * 1.6 - 5
                })
                .text(function() {
                  return d.id
                })
                .attr("text-anchor", "middle")
                .attr("fill", "#666")
            })
            .on("mouseout", function() {
              d3.select(this)
                .transition()
                .duration(100)
                .attr("r", d3.select(this).attr("r") / 1.6)
              showtext.text("")
            })
                     / / Add the left side of the part of the wrap layer
          let detail = cover.append("g")
          let showtext = svg.append("text")
            .text("")
            .attr("font-size", '14px')
Copy code

The final effect:


Oh~ Not bad~ You're done!


Intelligent Recommendation

d3 scale axis

Axis common method method format Explanation axisTop d3.axisTop() Scale value generated in the scale above axisBottom d3.axisBottom() Generating a scale value of the scale at the bottom axisLeft d3.ax...

D3-- scale, axis

When the array is too large or too small dimensions selected Linear scale A linear scale, a continuous range can be mapped to another section. To solve the problem of the width of a column chart, you ...

D3 scale and coordinate axis

The d3 used in this article is the v5 version. The scale can map data from "one interval" to "another interval". E.g[0, 1]Corresponds to[0, 300], When 0.5 is input, 150 is output. ...

D3.js-coordinate axis

Put coordinate in SVG Complete code:...

More Recommendation

D3 coordinate axis

Chapter 7 Coordinate axis The coordinate axis is a graphic that often appears in the visualization chart, consists of some column segments and scale. The coordinate axis is not a ready-made graphic el...

Echarts Click on the non-chart area (name, data on the X, Y axis)

Drilling a day, tears, write this article / (ㄒ o ㄒ) / ~~ Code: Here I got the text of the horizontal, the coordinates, and the actual situation is taken from params from params. Remarks: General click...

echarts chapter VI-scatter chart (bubble chart), the horizontal and vertical axis are in Chinese

Chart features and problems: 1. It needs to be two types of intersections. The horizontal axis and the vertical axis are both character types. The scatter chart found on the echarts official website, ...

D3 stacked bar chart

Background: d3 draws a stacked bar chart, each bar shows the percentage of multiple types data: effect: The column of the data is each x on the graph. Each bar is the value of each column displayed (v...

Line chart of d3.js

Line chart of d3.js Most of the online d3 is version 3 and 4, and many interfaces have changed. I am v5 version, it should be the latest. I hope to write all the pictures in the layout. My data is fro...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top