Indicate the highest value on Amcharts by drawing a line

I am currently working on plotting a graph that involves a significant amount of data. I have around 96 plots per day, and users can fetch data for up to a maximum range of 62 days. To implement this, I am utilizing Amcharts, but I have encountered an issue regarding displaying the highest value on the graph as a line. I am unsure if Amcharts has a built-in functionality to showcase the max value on the graph. If not, I will have to iterate through the entire object to insert the max value in each JSON array, which does not seem like an optimal solution.

Any assistance with this matter would be greatly appreciated.

Answer №1

Instead of iterating through all the data, we can leverage the chart's built-in calculations for min and max values on each value axis. By tapping into these auto-calculated values, we can easily add Guides to highlight the highest and lowest points.

The key here is to utilize the chart's "rendered" event. This event triggers after the chart is constructed, ensuring that the minimum and maximum values are already determined.

Within the value axis object, we have access to properties such as maxReal and minReal, which we can use to create Guides. These guides will visually display the highest and lowest points on the chart.

Below is a snippet of the code implementation:

chart.addListener( "rendered", function( event ) {
  // retrieve the chart and value axis
  var chart = event.chart;
  var axis = chart.valueAxes[0];

  // create guide for max value
  var guideMax = new AmCharts.Guide();
  guideMax.value = guideMax.label = axis.maxReal;
  guideMax.lineAlpha = 0.2;
  guideMax.lineThickness = 2;
  guideMax.lineColor = guideMax.color = "#00cc00";
  axis.addGuide(guideMax);

  // create guide for min value
  var guideMin = new AmCharts.Guide();
  guideMin.value = guideMin.label = axis.minReal;
  guideMin.lineAlpha = 0.2;
  guideMin.lineThickness = 2;
  guideMin.lineColor = guideMin.color = "#cc0000";
  axis.addGuide(guideMin);
} );

For a fully functional demonstration, refer to the complete working code example of the chart below:

// Chart configuration script
var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "path": "http://www.amcharts.com/lib/3/",
  "dataProvider": [
    // Data entries...
  ],
  "valueAxes": [{}],
  "graphs": [{
    // Graph configurations...
  }],
  "chartCursor": {
    // Cursor settings...
  },
  "dataDateFormat": "YYYY",
  "categoryField": "year",
  "categoryAxis": {
    // Category axis settings...
  }
});

// Event listener to add guides after rendering
chart.addListener("rendered", function(event) {
  var chart = event.chart;
  var axis = chart.valueAxes[0];
  
  // create guide for max value
  var guideMax = new AmCharts.Guide();
  guideMax.value = guideMax.label = axis.maxReal;
  guideMax.lineAlpha = 0.2;
  guideMax.lineThickness = 2;
  guideMax.lineColor = guideMax.color = "#00cc00";
  axis.addGuide(guideMax);
  
  // create guide for min value
  var guideMin = new AmCharts.Guide();
  guideMin.value = guideMin.label = axis.minReal;
  guideMin.lineAlpha = 0.2;
  guideMin.lineThickness = 2;
  guideMin.lineColor = guideMin.color = "#cc0000";
  axis.addGuide(guideMin);
});
#chartdiv {
  width: 100%;
  height: 500px;
}
<script src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="http://www.amcharts.com/lib/3/serial.js"></script>
<script src="http://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></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

Tips for converting HTML content into a properly formatted text file

I have a user interface where users can perform various actions using ajax calls. Once the ajax call is completed, the results are displayed in a div with an id = "log". I want to provide users with an option (a button labeled Export) so that when they hav ...

Encountering a 401 Error while trying to host an Angular app on Google Cloud Storage

I am currently facing an issue with deploying my Angular app to a Google Cloud Storage bucket. The bucket is public and set up to be served as a custom website via CNAME (test.example.com). The main page and 404 handler are mapped to index.html, but I am e ...

What is the best way to implement window.load in React Native?

I'm encountering an issue with a simple button on my page in Expo/React Native. When I try to navigate to a new page using window.open upon clicking the button, I receive an error message saying "undefined is not a function." Although I am utilizing ...

What is the best way to access Dynamic Array Data within a nested ng-repeat loop?

Dealing with JSON data that is deeply nested with strings and arrays, I am attempting to assign a true or false value for each day. Below is how I achieved this without a nested array: No Nested Array(Codepen 1) Here's the HTML: <ion-toggle ng-r ...

Troubleshooting Knockout.JS: Why self.myObservable is not working and the importance of code organization

My webpage uses knockout to handle a search field, dropdown selection, and page number. These elements are all initialized with default values for the first run or when the page is accessed. I'm encountering an error that says: "self.selectedTeamId i ...

Using Gson to deserialize a JSON feed from Facebook

I received a JSON response from Facebook structured as follows: { "data": [ { "id": "105458566194298_411506355589516", "message": "...", "type": "status", "created_time": "2012-11-25T17:26:41+0000", " ...

Why is it that servlets are unable to send custom JSON strings, and why is it that Ajax is unable to receive them?

I have developed a servlet that responds with a simple JSON list: public void addCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.log(Level.INFO, "Adding the category"); ObjectifyS ...

The React SwiperJs autoplay feature seems to be malfunctioning, as the swiper is not automatically

I have been utilizing the Swiper component in React from this link. However, I encountered an issue with setting it to autoplay as it doesn't auto swipe. Here is my attempted code: // Resource: https://swiperjs.com/get-started/ import React from &apos ...

using a function as an argument in the map method within a React component

I have a challenge where I am trying to display blog posts retrieved from my database. However, for each item, I also need to execute a download image function. I attempted to include the function within the .map function but encountered some errors. I am ...

Maintaining selected options in select lists while updating model data in Angular

New to Angular and exploring the Product object with Sku objects nested inside. An app allows users to fetch a product, resulting in the Product object being assigned to $scope.product: var app = angular.module('app', []); app.controller(&apos ...

Tips for accessing the HTML code of a TextBox in JavaScript while utilizing HTMLEditorExtender

Currently, I am utilizing the HTMLEditorExtender ajax tool on my website. The data is being saved in HTML format into the database and then retrieved within my project without any issues. However, there is a setback that I have encountered... When attemp ...

How to Implement Button Disable Feature in jQuery When No Checkboxes Are Selected

I need help troubleshooting an issue with a disabled button when selecting checkboxes. The multicheck functionality works fine, but if I select all items and then deselect one of them, the button disables again. Can someone assist me with this problem? Be ...

extracting ng-repeat values and passing them to the controller

I have created a form that dynamically increases whenever the user clicks on the add sale button Here is the HTML code: <fieldset data-ng-repeat="choice in choices"> <div class="form-group"> <label for="name"> Qu ...

The simple-ssh Node has successfully completed its operations without encountering any errors

I'm having trouble establishing a connection to an Ubuntu EC2 instance using the Node library called simple-ssh. Below is the code snippet I'm using: const SSH = require('simple-ssh') const fs = require('fs') var ssh = new ...

Learn the process of updating database information with AngularJS through a button click

As a newcomer to Angular, I am facing an issue in my mini project. The main goal of the project is to display data from a database. I have successfully set up the view to show current data by making API calls and connecting to the database. However, I am n ...

Retrieve responseObject from server using AFNetworking in case of failure

Trying to retrieve JSON data from the server using GET, but encountering an error due to line breaks "\n" in the data: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Unescaped control ch ...

Unraveling the Mystery of Passing Props in React.js

Currently taking an online course to learn React, I encountered a unique scenario where one property is attached to another property in this manner: this.props.property01(this.props.property02) The tutor briefly touched on this code line, leaving me quit ...

Assigning a value to a cell depending on a specific condition

Currently, I have a column connected to a field known as state. The possible values for this field are either S or L. My objective is to map these values as follows: S => Short, L => Long The binding is structured in the following way: $scope.grid ...

Can you spot the real transformation happening?

Is there a built-in way (possibly in one of the frameworks) to determine if a form has been modified from its initial values? The onchange event is not sufficient, as it triggers even if no real change has occurred (such as toggling a checkbox on and off ...

Having trouble initiating a "curl:localhost:3000" connection, receiving a URI Error message

Recently delving into the realm of node js, I have embarked on a journey to start up a server and experiment with an app designed to trim URLs. However, I find myself at an impasse. Environment: Windows Text Editor: VSCode Below is my code for index.js ...