logo
down
shadow

d3 Responsive bar chart


d3 Responsive bar chart

By : Mahadi Hasan
Date : November 18 2020, 03:01 PM
Does that help Here's a solution. Is this the desired output? Try resizing the window.
JS FIDDLE
code :
function resize() {
console.log('----resize function----');
// update width
width = parseInt(d3.select('#chart-area').style('width'), 10);
width = width - margin.left - margin.right;

height = parseInt(d3.select("#chart-area").style("height"));
height = height - margin.top - margin.bottom;
console.log('----resiz width----'+width);
console.log('----resiz height----'+height);
// resize the chart

    x0.rangeRoundBands([0, width], .5);
x1.domain(rateNames).rangeRoundBands([0, x0.rangeBand()]);
y.range([height, 0]);

yAxis.ticks(Math.max(height/50, 2));
xAxis.ticks(Math.max(width/50, 2));

d3.select(svg.node().parentNode)
  .style('width', (width + margin.left + margin.right) + 'px');

svg.selectAll('.g')
  .attr("transform",function(d) { 
    return "translate(" + x0(d.category) + ",0)"; 
    });
    svg.selectAll('.g').selectAll("rect").attr("width", x1.rangeBand())
    .attr("x", function(d) { return x1(d.rate); })
svg.selectAll(".legend rect")
  .attr("x", width - 18);
svg.selectAll('.legend text')
    .attr("x", width - 24)  
svg.select('.x.axis').call(xAxis.orient('bottom')); 
}
x0.rangeRoundBands([0,width],.5);
x1.domain(rateNames).rangeRoundBands([0,x0.rangeBand()]);
y.range([height, 0]);


Share : facebook icon twitter icon
Responsive Chart.js Doughnut Chart with minimum height

Responsive Chart.js Doughnut Chart with minimum height


By : bjlukubku
Date : March 29 2020, 07:55 AM
Does that help I wasn't really able to "solve" this per say. But what appears to be happening is the red bordered div is just slightly to small for the responsive chart to get to the appropriate size for that div width I think?
What I ended up doing is putting the charts (responsive mode on) in 2 bootstrap columns and it ended up doing the looking very nice.
code :
<div id="circle_chart" class="col-md-5"><canvas></canvas></div>
<div id="chart_container" class="col-md-7"><canvas></canvas></div>
$("#circle_chart").css("margin-top", (($("#chart_container").height()-$("#circle_chart").height())/2)+"px");
Chart.js responsive option - Chart not filling container

Chart.js responsive option - Chart not filling container


By : user2849484
Date : March 29 2020, 07:55 AM
I wish this help you Sorry about this, but I realized that the version I was using was outdated. For future reference, I found an up to date version of chart.js here.
code :
//cdn.jsdelivr.net/chart.js/1.0.1-beta.4/Chart.min.js
show multiple responsive chart in the same page using chart.js

show multiple responsive chart in the same page using chart.js


By : Hub Nguyen
Date : March 29 2020, 07:55 AM
like below fixes the issue You should abandon using tables and use DIVs instead.
Here is an example using a responsive 2x2 grid where each DIV contains your canvas. The css used was taken from this answer and modified for 2x2.
code :
<div class="w">
  <section>
    <div>
      <canvas id="canvasLeftTop"></canvas>
    </div>
    <div>
      <canvas id="canvasRightTop"></canvas>
    </div>
    <div>
      <canvas id="canvasLeftBottom"></canvas>
    </div>
    <div>
      <canvas id="canvasRightBottom"></canvas>
    </div>
  </section>
</div>
html, body {
  margin:0;
}
.w{
  overflow:hidden;
}
section div {
  float: left;
  height: 24vw;
  margin: 1%;
  width: 46%;
  border-style: solid;
  border-width: 1px;
}
section {
  margin:-1%;
  padding:20px;
}
How to position chart details next to a chart responsive for multiple devices?

How to position chart details next to a chart responsive for multiple devices?


By : John Alexander Satmi
Date : March 29 2020, 07:55 AM
I wish did fix the issue. IF you could use a static image then, it would be better to draw the image on the chart itself, rather than adding it as a separate element and do all the positioning stuff.
To properly draw the image on the chart, you can use the following plugin :
code :
Chart.plugins.register({
   chartUpdated: false,
   preload: false,
   imageLoaded: false,
   image: new Image(),
   loadImage: function(chart) {
      var _this = this;
      this.image.onload = function() {
         _this.imageLoaded = true;
         _this.drawScale(chart, this);
      }
      this.image.src = 'https://i.imgur.com/YDRy5jA.png'; // image URL
   },
   drawScale: function(chart, img) {
      var ctx = chart.ctx,
         y_axis = chart.scales['y-axis-0'],
         topY = y_axis.top,
         bottomY = y_axis.bottom,
         scaleHeight = bottomY - topY,
         scaleOffset = scaleHeight * (img.width / img.height * 100) / 100;
      chart.options.layout = {
         padding: {
            left: scaleOffset
         }
      }
      ctx.drawImage(img, 0, topY, scaleOffset, scaleHeight);
      if (!this.chartUpdated) {
         chart.update();
         this.chartUpdated = true;
      }
   },
   afterDraw: function(chart, ease) {
      if (!this.preload) {
         this.loadImage(chart);
         this.preload = true;
      } else if (this.imageLoaded) this.drawScale(chart, this.image);
   }
});
Chart.plugins.register({
   chartUpdated: false,
   preload: false,
   imageLoaded: false,
   image: new Image(),
   loadImage: function(chart) {
      var _this = this;
      this.image.onload = function() {
         _this.imageLoaded = true;
         _this.drawScale(chart, this);
      }
      this.image.src = 'https://i.imgur.com/YDRy5jA.png'; // image URL
   },
   drawScale: function(chart, img) {
      var ctx = chart.ctx,
         y_axis = chart.scales['y-axis-0'],
         topY = y_axis.top,
         bottomY = y_axis.bottom,
         scaleHeight = bottomY - topY,
         scaleOffset = scaleHeight * (img.width / img.height * 100) / 100;
      chart.options.layout = {
         padding: {
            left: scaleOffset
         }
      }
      ctx.drawImage(img, 0, topY, scaleOffset, scaleHeight);
      if (!this.chartUpdated) {
         chart.update();
         this.chartUpdated = true;
      }
   },
   afterDraw: function(chart, ease) {
      if (!this.preload) {
         this.loadImage(chart);
         this.preload = true;
      } else if (this.imageLoaded) this.drawScale(chart, this.image);
   }
});

Chart.plugins.register({
   beforeDraw: function(chart) {
      var ctx = chart.chart.ctx,
         x_axis = chart.scales['x-axis-0'],
         topY = chart.scales['y-axis-0'].top,
         bottomY = chart.scales['y-axis-0'].bottom;
      x_axis.options.gridLines.display = false;
      for (i = 0; i <= x_axis.ticks.length; i++) {
         var x = i === x_axis.ticks.length ?
            x_axis.right :
            x_axis.getPixelForValue(x_axis.ticks[i]);
         ctx.save();
         ctx.beginPath();
         ctx.lineWidth = 1;
         ctx.strokeStyle = x_axis.options.gridLines.color;
         ctx.moveTo(x, topY);
         ctx.lineTo(x, bottomY);
         ctx.stroke();
         ctx.restore();
      }
   }
});

var options = {
   type: 'bar',
   data: {
      labels: ["1", "2", "3", "4", "5"],
      datasets: [{
         borderWidth: 2,
         borderColor: "#5d5d5d",
         pointBorderColor: "#5d5d5d",
         pointBackgroundColor: "#5d5d5d",
         pointBorderWidth: 5,
         type: 'line',
         data: [26, 26, 33, 28, 30],
         fill: false,
         lineTension: 0
      }, {
         borderWidth: 3,
         pointBorderColor: "#b8b8b8",
         pointBackgroundColor: "#b8b8b8",
         pointBorderWidth: 10,
         type: 'line',
         data: [26, 26, 29, 28, 29],
         fill: false,
         lineTension: 0
      }, {
         data: [0, 0, 0, 0, 0],
         fill: false,
         lineTension: 0
      }]
   },
   options: {
      responsive: true,
      //maintainAspectRatio: false,
      hover: {
         mode: null
      },
      legend: {
         display: false
      },
      tooltips: {
         enabled: false
      },
      hover: {
         mode: null
      },
      scales: {
         xAxes: [{
            gridLines: {
               // drawBorder: false,
            },
         }],
         yAxes: [{
            display: false,
            ticks: {
               suggestedMin: 0,
               max: 60,
               beginAtZero: true
            }
         }]
      }
   }
}

var ctx = document.getElementById('canvas').getContext('2d');
var myChart = new Chart(ctx, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="canvas" width="400"></canvas>
Chart.JS full-width, responsive doughnut chart with Bootstrap

Chart.JS full-width, responsive doughnut chart with Bootstrap


By : wikiwikiweb
Date : March 29 2020, 07:55 AM
wish of those help If you want to make it responsive and preserve aspect ratio, 2 things to consider:
1) In options:
Related Posts Related Posts :
  • Bootstrap DatePicker format mm/yyyy set max month
  • Firebase cloud firestore + auth: write only for signed in users
  • Getting jQuery.data functionality without jQuery
  • How to get incrementing serial numbers for new and removed items for jQuery sortable?
  • Highlighting a word or sentence in iframe, using javascript/Jquery
  • Calculate what percentage of a specific element has been scrolled into view
  • Knockout autocomplete with jquery doesn't allow to select custom value
  • react native - react-native-maps performance slow on iOS
  • ajax fallback when no internet connection
  • Show Textbox based on RadioButton selection or value when Page Loads
  • JS maximum call stack exceeded
  • Resetting object key values
  • How can i disable the toggle, preventing user to tap/click it?
  • How to include javascript file into LOV popups on apex oracle?
  • Javascript Angular 4 eventEmitter with ngClass
  • Webpack - module not found even though module exists
  • How to display following values using vue js?
  • Regexp: Allow only use of a few words and only once per word
  • CCapture.js webm video blacked out
  • Using a HTML hyperlink to call a JS function on the parent element
  • Return undefined from existing property in javascript model
  • What is the Difference Between These two jQuery Code Snippets?
  • How to get Network Speed in WebRTC
  • How to get text from selected value in a dropdownlist which is js based
  • window is not defined angular universal third library
  • Angularjs ng-repeat stylization depending on previous value
  • Trying to implement Fittext.js
  • Calculate number of match in array Lodash
  • Jquery Smooth Scroll Using Offset.top
  • How to extract data to React state from CSV file using Papa Parse?
  • How to add unique links to google maps markers
  • How to use if condition in a tool bar in java script
  • Ajax filter in django not showing in HTML
  • data collection with Javascript
  • Rotate image on lightbox2 load
  • Prevent body from scrolling when a Pop-Up is open
  • How to copy files that do not need to be compiled in Gulp?
  • Array not assigned to variable? How does this work and what exactly is it doing?
  • Sorting associative array of objects in javascript
  • Changing Icon in Sap.m.tree having CustomTreeItem
  • Merge two array of objects based on a key
  • javascript in css not working
  • Passing only clicked element to onClick function - reactjs
  • React boilerplate doesn't load js files in the index.html
  • is Child service inside child component visible in the Parent component?
  • Check if data attribute value equals a string
  • How to get value of child tag of a button tag
  • How to access subjects of selected mails in Apple Mail using JavaScript?
  • How to get all dynamically set inline-style CSS in jQuery?
  • Error: Module "html" does not provide a view engine (Express)
  • Random Image in <Div> from array
  • Slider with touch function
  • ReactJS Component Architecture Problems / Nested Components or Single Component Manager
  • Javascript: Caching within Closure doesn't work
  • HTM5 Canvas Drawing App: How Do I Select The Color?
  • Assigning Events using HTML DOM
  • html5 getUserMedia() portrait mode
  • How to avoid 'headers already sent' within Promise chain?
  • Get a result from a react native app integrated into an existing android app
  • Why does the value of input field return undefined
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org