Issues encountered when utilizing a provider alongside a controller for implementing Highcharts visualizations in angularjs

I've been working on an Angular web application that incorporates highcharts (highcharts-ng) integration.

My approach was to set up a factory provider where I defined my chart configuration options object:

angular.module('socialDashboard')

   .factory('SentimentChartFactory', function() {
   var sentimentGraph = {
      options: {
      chart: {
      type: 'area',
      backgroundColor: false,
      height: 450,
      zoomType: 'x'
    }
  },
  colors: ['#d1d5d8', '#954145', '#41a85f'],
  xAxis: {
    type: 'datetime',
    dateTimeLabelFormats: { // don't display the dummy year
      month: '%e. %b',
      year: '%b'
    },
    gridLineWidth: 1
  },
  yAxis: {
    title: {
      text: 'Sentiment %'
    },
    gridLineWidth: 0,
    labels:
    {
      enabled: false
    }
  },
  series: [
    {
      name: 'Basic Neutral',
      data: [
        [Date.UTC(2014, 10, 10), 60],
        [Date.UTC(2014, 10, 11), 55],
        [Date.UTC(2014, 10, 12), 50],
        [Date.UTC(2014, 10, 13), 60],
        [Date.UTC(2014, 10, 14), 55],
        [Date.UTC(2014, 10, 15), 60],
        [Date.UTC(2014, 10, 16), 55],
        [Date.UTC(2014, 10, 17), 50],
        [Date.UTC(2014, 10, 18), 60],
        [Date.UTC(2014, 10, 19), 55],
       ]
     }
    ],
    loading: false
    };
    return sentimentGraph;
});

Subsequently, I created a controller for my graphs to retrieve the options object from my factory provider:

angular.module('socialDashboard')

    .controller('SentimentChartController', function ($scope, $http, SentimentChartFactory) {

        // Configuration for summary page    
        $scope.SentimentChartSummaryConfig = SentimentChartFactory;
        $scope.SentimentChartSummaryConfig.options.chart.height = 250;

        // Configuration for Sentiment Page
        $scope.SentimentChartConfigMain = SentimentChartFactory;
        $scope.SentimentChartConfigMain.options.chart.height = 450;
});

This is where I instantiate my chart:

Summary Page:

<div ng-controller="SentimentChartController">
    <highchart id="{{link + '_chart'}}" config="SentimentChartSummaryConfig"></highchart>
</div>

Sentiment Page: (Different view)

<div ng-controller="SentimentChartController">
    <highchart id="{{link + '_chart'}}" config="SentimentChartConfigMain"></highchart>
</div>

The issue I'm facing is that despite assigning a different value to the config attribute, both graphs are appearing with the same height (450). What could be causing this and how can I resolve it?

Answer №1

The issue arises because both SummaryConfig and MainConfig reference the same object, which is the SentimentChartFactory object.

To resolve this, it is recommended to have the service return a new object every time:

angular.module('socialDashboard')
.factory('SentimentChartFactory', function() {
    return {
        getConfig: function() {
            return {
                options: {
                    chart: {
                        type: 'area',
                        backgroundColor: false,
                        height: 450,
                        zoomType: 'x'
                    }
                },
                colors: ['#d1d5d8', '#954145', '#41a85f'],
                xAxis: {
                    type: 'datetime',
                    dateTimeLabelFormats: {
                        month: '%e. %b',
                        year: '%b'
                    },
                    gridLineWidth: 1
                },
                yAxis: {
                    title: {
                        text: 'Sentiment %'
                    },
                    gridLineWidth: 0,
                    labels: {
                        enabled: false
                    }
                },
                series: [{
                    name: 'Basic Neutral',
                    data: [
                        [Date.UTC(2014, 10, 10), 60],
                        [Date.UTC(2014, 10, 11), 55],
                        [Date.UTC(2014, 10, 12), 50],
                        [Date.UTC(2014, 10, 13), 60],
                        [Date.UTC(2014, 10, 14), 55],
                        [Date.UTC(2014, 10, 15), 60],
                        [Date.UTC(2014, 10, 16), 55],
                        [Date.UTC(2014, 10, 17), 50],
                        [Date.UTC(2014, 10, 18), 60],
                        [Date.UTC(2014, 10, 19), 55],
                    ]
                }],
                loading: false
            }
        }
    };
});

Afterwards, you can use it like this:

$scope.SentimentChartSummaryConfig = SentimentChartFactory.getConfig();
$scope.SentimentChartSummaryConfig.options.chart.height = 250;

$scope.SentimentChartConfigMain = SentimentChartFactory.getConfig();
$scope.SentimentChartConfigMain.options.chart.height = 450;

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

Using Query strings in JavaScript: A Quick Guide

I recently completed a calculator project with only two pages. However, I am struggling to figure out how to pass input field values from one page to another. Despite trying multiple methods, nothing seems to be working. Does anyone know how to successful ...

Utilize toggle functionality for page rotation with rxjs in Angular framework

Managing a project that involves a container holding multiple cards across different pages can be overwhelming. To address this, the screen automatically rotates to the next page after a set time interval or when the user presses the space bar. To enhance ...

Once an email address is entered, kindly instruct the driver to press the tab key twice for navigation

Adding a user to a website involves entering an email address first, which is then checked against the server's list of users. However, the issue arises when the email validation doesn't occur until clicking outside the input box or pressing tab ...

Securing Communication with HTTPS in Express JS

After purchasing an AlphaSSL from a hosting provider, I received the following files: domain.csr.crt domain.interCert.crt domain.PKCS7Cert.crt domain.rootCert.crt domain.X509Cert.crt However, in order to enable HTTPS on Node.JS using Express, I am aware ...

When the draggable item is released near the drop zone, allow drag and drop to combine

In my latest project, I created a fun game that involves matching different shapes. The goal is to drag the correct figure next to its corresponding shadow figure for a perfect match. Currently, if you partially overlap the square with its shadow, the game ...

Error in cloned selections with bootstrap-selectpicker showing one less item

When duplicating a bootstrap-select dropdown, the duplicate dropdown appears to be offsetting selections by 1. For example, if the second option is clicked, the first one is actually selected. Here is an illustration: If "New Castle" is clicked in the ...

Ways to center text within a nested div in a parent div with table-cell styling

The issue I am facing involves a parent div with the CSS property display: table;. Nested inside is a child div with display: table-cell;. Despite applying text-align: center;, the text within the second div does not align to the center as expected. Here ...

Is it possible to target elements based on a specific CSS3 property they use?

Is there a method to target all elements in the DOM with a border-radius other than 0? If anyone has suggestions, it would be greatly appreciated! ...

Can you explain the significance of this regular expression?

I'm trying to decipher the meaning of this regular expression. Can anyone help? "^[A-Z]{3}-[4-7]\d{2,4}\$$" My understanding is that it must start with exactly 3 uppercase letters and end with a sequence of 2, 3, or 4 digits (although I a ...

Issues with response functionality in Node.js (Openshift) using express

I am currently working with OpenShift and Node.js to calculate the average rating for each result. However, I am facing an issue where the response is not being displayed even though the console logs show the correct data. The console displays 3.9454323, ...

Having trouble locating the namespace for angular in typescript version 1.5

I am using Angular 1.5 with TypeScript and have all the necessary configurations in my tsconfig.json file. However, when I run tslint, I encounter numerous errors in the project, one of which is: Cannot find namespace angular My tsconfig.json file looks ...

The proper method for organizing a nested array object - an obstacle arises when attempting to sort the array

I have a collection of data fetched from Web API 2.2 stored in an Angular array as objects. Each object represents a Client and includes properties like name, surname, and a collection of contracts assigned to that client. Here is the interface definition ...

What is the best way to choose a random ID from a table within specified time intervals in MySQL?

I need to retrieve a random ID from the table nodes within the last 15 seconds. I attempted the following code, but it produced a lengthy output: const mysql = require('mysql'); const connection = mysql.createConnection({ host ...

Unable to attach a javascript eventListener to a newly created element

I've utilized JavaScript to generate 625 <div class="box"><div> elements and now I'm trying to attach an event listener to each box. The boxes are being created successfully, however, the listener does not seem to be working ...

Problem: Vue 3 build does not generate service worker

After adding the "@vue/cli-plugin-pwa": "^4.5.12" to my package.json and setting up workbox configuration in my vue.config.js, I encountered an issue. When running npm run build:prd with the script vue-cli-service build --mode prod.exam ...

Exploring additional parameters within express.js

I'm currently working on creating an email sender that includes all necessary components such as 'from', 'to', 'subject', 'textBody', etc. However, I am facing a challenge in setting optional parameters in expre ...

Using Javascript to define cookie values

I'm attempting to use JavaScript to set a cookie that will instruct Google Translate to select the language for the page. I've experimented with setting the cookie based on my browser's cookie selection of a language. <script> $(doc ...

Is it feasible to implement automatic window closure on the alternate website?

Imagine I have a website, let's say http://myownsite.com, and my desire is to execute a script that will open a new window on Safari or any other browser for a site which I do not own: http://theothersite.com If I lack control over that specific site ...

Having trouble updating text in a PDF using NodeJS and hummus library

How can NodeJS be used to replace a string in a PDF file? offers a solution for text replacement in a PDF document. However, when applying the same code, an issue arises where the replaced text is visible in the source code of the PDF but does not render p ...

Variability in Swagger parameter declaration

Is it possible to specify input parameters in Swagger with multiple types? For example: Consider an API that addresses resources using the URL http://localhost/tasks/{taskId}. However, each task contains both an integer ID and a string UUID. I would like ...