Master the art of adjusting chart width on angular-chart with the help of chart.js

I am currently using angular-chart along with Angular and chart.js to create multiple charts on a single page. However, I am facing an issue where each chart is taking up the entire width of the screen. I have tried various methods to limit the width based on a ratio of "window.innerWidth" without any success. The chart displays correctly but does not adjust its width as desired.

Below is a snippet of my current HTML:

<div ng-repeat="dataCenterData in dataCenterDataList">
<div ng-style="{'width': windowWidth / 2}">
<canvas id="daily-chart-{{dataCenterData.dataCenter}}"
class="chart chart-bar" chart-data="dataCenterData.data"
chart-labels="dataCenterData.labels"
chart-series="dataCenterData.series"
chart-options="dataCenterData.options"></canvas>
</div>
<div ng-style="{'width': windowWidth / 2}">
<canvas id="last30Minutes-chart-{{dataCenterData.dataCenter}}"
class="chart chart-line"
chart-data="last30MinutesDataCenterDataList[$index].data"
chart-labels="last30MinutesDataCenterDataList[$index].labels"
chart-series="last30MinutesDataCenterDataList[$index].series"
chart-options="last30MinutesDataCenterDataList[$index].options"></canvas>
</div>
</div> 

In my controller, I have "$scope.windowWidth = window.innerWidth" which sets the value to 1432 in my current test case.

Answer №1

Utilizing the Bootstrap grid system allows you to showcase charts in two columns within the same row. In Bootstrap, a row consists of 12 columns, so for a two-column layout, we divide them into two divs each spanning 6 columns like this:

<div class="container" ng-app="app" ng-controller="ChartCtrl">
    <div class="row">
        <div class="col-md-6">
            Column1
        </div>
        <div class="col-md-6">
            Column2
        </div>
    </div>
</div>

Demo: https://jsfiddle.net/codeandcloud/vz4qhqpw/show/
Source Code: https://jsfiddle.net/codeandcloud/vz4qhqpw/

Required Dependencies:

  1. jQuery.js
  2. Bootstrap.js
  3. Bootstrap.css

If you prefer displaying them in two separate rows, you can achieve it by doing something like this:

<div class="container" ng-app="app" ng-controller="ChartCtrl">
    <div class="row">
        <div class="col-md-offset-3 col-md-6">
            Column1
        </div>
    </div>
    <div class="row">
        <div class="col-md-offset-3 col-md-6">
            Column2
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

precise measurement of cell size

Here's the scenario: I have a table in ng-grid set up like this: var templateImage = '<div class="ngCellText" ng-class="col.colIndex()"><img src="images/{{row.getProperty(\'faseXD[0].fase\')}}.png"></div>&a ...

The link appears to be broken when trying to access the notFound component in Next.js version 13

In my Next.js 13.4 app directory, I added a not-found.tsx component that functions correctly when entering the wrong route: import Link from 'next/link' function NotFound() { return ( <section> 404, page not found ...

What is the best way to switch out the characters 'a' and 'b' in a given string?

Let's say we have the following text. Lorem ipsum dolor sit amet The revised text should look like this: merol merol merol merol Is there a predefined function in JavaScript that can help us replace characters like this within a string? ...

Confirm that the form is valid when a specific requirement is fulfilled

I am currently working on a credit card project that involves using a JavaScript function called validateCreditCard to validate credit cards and determine the type. The idea is to store the credit card type as the value of a hidden input field called cardT ...

What is the process of setting up various initialization parameters for a DataTable?

Here is a snippet of JavaScript code I have been using to initialize a DataTable: $(document).ready(function() { $('#example').DataTable( { dom: 'Bfrtip', buttons: [ 'copyHtml5', &a ...

Encountering AngularJS promise data parsing issues

I am trying to work with promises in AngularJS. However, I encountered an error while parsing the backend response in AngularJS. What could be the issue here? This is the HTML code: <div ng-app="clinang" ng-controller="pacientesCtrl"> <a ...

Modifying all occurrences of a specified string in an object (or array) - JavaScript

Is there a more efficient way to search through and replace all instances of a given string in a JavaScript object with unknown depth and properties? Check out this method, but is it the most optimal solution? var obj = { 'a' : 'The foo ...

Verify the length of an array within an object using JavaScript

I am facing a problem with an object. Here is what it looks like: const array = { "entities": [ { "annexes": [ { "buildingUniqueIds": [] }, { ...

What is the best way to prevent event propagation in d3 with TypeScript?

When working with JavaScript, I often use the following code to prevent event propagation when dragging something. var drag = d3.behavior.drag() .origin(function(d) { return d; }) .on('dragstart', function(e) { d3.event.sourceEvent ...

The console is displaying the array, but it is not being rendered in HTML format in AngularJS

Can you please review my App.js file and let me know if there are any mistakes? I have provided the necessary files index.html and founditemtemplate.html below. Although it is returning an array of objects in the console, it is not displaying them as inten ...

Utilize AngularJS to interface with the asp.net MVC model

When working with asp.net MVC and angularjs, I encountered an issue. I was able to successfully convert my model into a javascript object using var model = @Html.Raw(Json.Encode(Model));. This allowed me to access the object in the console on Chrome withou ...

Is there a pub/sub framework specifically designed for managing events in Angular?

Having a background in WPF with Prism, I am familiar with the IEventAggregator interface. It allows you to define events that can be subscribed to from controllers and then triggered by another controller. This method enables communication between controll ...

What could be the reason for a variable not being assigned a value following the xmlhttp.responseText?

During the validation process of a send-fax form, I am checking if a fax has been previously sent using our software fax package. This involves a simple query to a table executed by a script, which will return text if a previous fax exists or blank if not. ...

Is history.pushState capable of more than just making an xhr request?

I've hit a roadblock in my current project. The issue I'm facing is getting a registration page to load. The XHR request is returning the output of the PHP code, which is causing problems. My goal is to have it load as a document rather than an ...

What could be the reason that Vue is not evaluating anything when the directive @click is specified as "true && method"?

Consider the following scenario: LandingPage.vue <template> <button @click="handleClick">Click Me</button> </template> <script> export default { methods: { handleClick() { this.$emit("click"); } } }; < ...

Clicking the set button in Jquery mobile datebox resets the time

I am experimenting with the jquery mobile datebox (courtesy of Jtsage) to select time and date. Unfortunately, every time I reset the time by clicking the set button, I am unable to retrieve the correct time. Below are my code snippets: $.extend($.jtsag ...

Adjust the content within an HTML div by utilizing AngularJS

Snippet of AngularJs code to display different content based on selection: <select> <option value="0">--Select--</option> <option value="1">Individual</option> <option value="2"> ...

Javascript and CSS combo for creating a stylish fade effect

Hey everyone, I have a design challenge where I need to change the color of a picture inside a box when the user hovers over it. I have four boxes like this and my goal is to make everything else fade out when a user hovers over a specific box. Anyone kn ...

The window.open function is returning a null value after attempting to open the specified

Is there a way to prevent users from opening more than one IFrame window for my application? I have included the following code: <html> <head> <title>Testing Window Opening Limitation</title> <meta http-equiv="Content-Type" cont ...

How can you check the status of a user in a Guild using Discord JS?

Is there a way to retrieve the online status of any user in a guild where the bot is present? Although I can currently access the online status of the message author, I would like to be able to retrieve the online status of any user by using the following ...