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

Establish a secondary resource group specifically for the nested template

Attempting to deploy a template for setting up a site-to-site VPN and connecting a virtual machine to Azure using nested templates. Each template functions independently as well as together when nested. The issue arises when trying to deploy the site-to-s ...

Troubles Encountered in Converting Date and Time to JSON Format

I am currently facing an issue where I need to convert a list<T> into JSON data and then bind it to Jqgrid. However, the problem I am encountering is that the list collection includes one column with DateTime values. Upon inspecting the resultant J ...

Extract branch, path, and URL from the .gitmodules file by utilizing JavaScript Regex

Is there a way to extract branch, path, and URL details from the .gitmodules file using JavaScript Regex? .gitmodules [submodule "PATH"] path = <PATH> url = <URL> [submodule "PATH"] path = <PATH> url = <URL> ...

Can you explain the process of variable allocation?

I have a query regarding the following JavaScript code snippet. It might be a basic question, but I'm seeking clarification. // 'response' contains JSON data received from the backend: ....then(response => { this.myVar = response.data; ...

What could be causing the span class data to not be retrieved by the getText method

Looking to retrieve the value of a span class called 'spacer-right big project-card-measure-secondary-info' <span class="spacer-right big project-card-measure-secondary-info">1</span> snippet of code: browser.waitForEl ...

Is there a way to merge two separate on click functions into one cohesive function?

I currently have two separate onclick functions as shown below. They are functioning properly but I am considering combining them for optimization purposes. Essentially, clicking on xx displays certain elements, hides others, and adds a class. Clicking o ...

Trying to replace all instances of a word in an HTML document with a <span> element, but only for <p>, <span>, and <div> tags. It shouldn't work if the parent node already contains

Here is the HTML code snippet I am working with: <div> hello world <p> the world is round <img src="domain.com/world.jpg"> </p> </div> I am looking to replace the word "world" (or any mixed case variations) with < ...

"Although the ajax request was successful, the post data did not transfer to the other

i am working with a simple piece of code: var addUser = "simply"; $.ajax({ type: 'POST', url: 'userControl.php', data: {addUser: addUser}, success: function(response){ alert("success"); } }); on the page use ...

Reactjs and Redux encounter an Unhandled Rejection with the error message stating "TypeError: Cannot read property 'data' of undefined"

Encountering an error while implementing authentication with react + redux. When attempting to register a user in the redux actions using async / await, I consistently receive this error within the catch block. Here is the snippet of the actions code: imp ...

Display a "Loading" image in the gallery before anything else loads

Can a animated loading gif be implemented to load before the gallery images, or would it serve no purpose? The image will be loaded as a background using CSS. <link rel="stylesheet" href="loading.gif" /> Appreciate your help! ...

Applying styled text to a Node.js chat application

I developed a chat application using node.js which allows users to enter a username and send messages. The messages are displayed in a <ul> format showing "username: message". I was looking for a way to make the username appear bold and in blue color ...

An error message stating "ReferenceError: str is not defined" was encountered in Firefox while using the Gettext Library

The gettext.js library seems to be giving me trouble. When I try to run the "index.html" file, an error message saying "ReferenceError: str is not defined" pops up. this.LCmessages[lang] = new jsGettext.Parse(str); I'm not sure where the str variabl ...

Passing parameters to Next.js pages

Here is my function: export async function getServerSideProps({ req }: any) { const user = ( await axios.get("http://localhost:4000/api/auth/status", { withCredentials: true, headers: { Cookie: `connect.sid=${req.cookies["c ...

How can an array be concatenated using tabs?

I am currently in the process of integrating with an old system that requires a value to be sent via a GET request as a tab delimited string. I have my data stored in an array, but I am facing difficulty when attempting to properly format it using the join ...

Jackson JSON throws an error stating that there is no appropriate constructor found for a type when an Enum is used

I have a class structured like this: public class Content { public enum Type { TEXT, URL, FILE } public enum Rendering { MARKDOWN, HTML, PLAIN, AUTO } public final Type type; pu ...

Achieving different results by modifying an array within a forEach loop

I am currently working on a forEach() function that increments an array by one for each iteration. The problem I am facing is that whenever I try to display the value of the number variable in the browser's console, it only shows me the final state i ...

Using Vue.js to pass a variable from a parent component to a child component

Parent component: ShowComment Child component: EditComment I'm attempting to pass the value stored in this.CommentRecID to the child component. In the template of ShowComment, I have included: <EditComment CommentRecID="this.CommentRecID" v-if= ...

Issue with setInterval function execution within an Angular for loop

My goal is to dynamically invoke an API at specific intervals. However, when attempting to utilize the following code snippet in Angular 7, I encountered issues with the interval timing. I am seeking a solution for achieving dynamic short polling. ngOnIn ...

Guide on displaying the length of an observable array in an Angular 2 template

I am working with an observable of type 'ICase' which retrieves data from a JSON file through a method in the service file. The template-service.ts file contains the following code: private _caseUrl = 'api/cases.json'; getCases(): Obs ...

Enhance the functionality of the custom transaction form in NetSuite by incorporating new actions

I'm currently working on incorporating a new menu option into the "Actions" menu within a custom transaction form in NetSuite. While I can successfully see my selection in the Actions Menu on the form, I'm running into an issue with triggering th ...