Exploring the functionality of negative numbers within chartist.js

Is there a way to display negative numbers in chartist.js? It seems that the library does not support negative numbers. Here is my code:

        var _4date = 'D';
        var _3date = 'C';
        var _2date = 'B';
        var _1date = 'A';

        var _4all_scores = '-100';
        var _3all_scores = '476';
        var _2all_scores = '649';
        var _1all_scores = '0';

        var _4comments_scores = '-100';
        var _3comments_scores = '20';
        var _2comments_scores = '0';
        var _1comments_scores = '0';


        var _4transactions_scores = '0';
        var _3transactions_scores = '456';
        var _2transactions_scores = '649';
        var _1transactions_scores = '0';



        var chart = new Chartist.Line('.ct-chart', {
            labels: [_1date, _2date, _3date, _4date],
            series: [
                [_1all_scores, _2all_scores, _3all_scores, _4all_scores],
                [_1comments_scores, _2comments_scores, _3comments_scores, _4comments_scores],
                [_1transactions_scores, _2transactions_scores, _3transactions_scores, _4transactions_scores]
            ]
        }, {
            low: 0,
            showArea: true,
            showPoint: false,
            fullWidth: true
        });

        chart.on('draw', function(data) {
            if(data.type === 'line' || data.type === 'area') {
                data.element.animate({
                    d: {
                        begin: 2000 * data.index,
                        dur: 2000,
                        from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
                        to: data.path.clone().stringify(),
                        easing: Chartist.Svg.Easing.easeOutQuint
                    }
                });
            }
        });

https://i.sstatic.net/le850.jpg

Answer №1

Issue Resolved:

The solution was to delete the following line:

low: 0

Answer №2

For those in need of assistance...

I encountered an issue with my area chart which displayed only negative numbers, and I had set the following:

var low = Math.min(...data) * 0.75;
var high = Math.max(...data) * 1.25;

However, this setup worked well for positive numbers, but it caused the chart to be clipped, rendering it invisible. To rectify this for charts with solely negative numbers, I made the adjustment as follows:

var low = Math.max(...data) * -1.25;
var high = Math.min(...data) * -0.75;

The reason behind this modification is that I reversed my chart using value * -1, since the standard function for mirroring the chart was ineffective.

As a result, my method evolved into something like this:

    function chartOptions(data, reversed=false) {

        if(reversed) {
            var low = Math.max(...data) * -1.25;
            var high = Math.min(...data) * -0.75;
        } else {
            var low = Math.min(...data) * 0.75;
            var high = Math.max(...data) * 1.25;
        }

        return {
            showPoint: true,
            showLine: true,
            showArea: true,
            fullWidth: true,
            showLabel: false,
            low: low,
            high: high
        }
    }

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

Converting PHP array to a JavaScript object

I am facing a scenario like this: Having JSON data: [{ "1377412272": { "user_id": "1374050643", "date": "2013-08-24", "ip": "::1" } }, { "1377412279": { "user_id": "137405064 ...

Troubleshooting a problem with a personalized webkit scrollbar in Chrome

Using the CSS property scroll-snap-type: y mandatory; to customize my scrollbar with the following styles: ::-webkit-scrollbar { width: 12px; background: transparent; } ::-webkit-scrollbar-track { background-color: rgba(0, 0, 0, 0.5); -webkit-box-s ...

Alpha-Beta Pruning Minimax Algorithm Fails to Determine the Best Move in Tic-Tac-Toe Artificial Intelligence

I have been developing a Tic-Tac-Toe AI system that uses the Alpha-Beta Pruning Minimax algorithm to determine the best move for the AI player (X) on the game board. Unfortunately, I am running into an issue where the algorithm is not returning the correct ...

Setting a flag in jQuery to halt the submission loop

Hey there, I'm grappling with a little logic issue and could use some guidance. The code I have seems to be functioning well, but I'm stuck on how to implement a flag to prevent an endless loop. So, here's the scoop: I have a login page whe ...

Learn how to increase a value with each dynamically clicked button in JavaScript

Whenever I try to increment the value by clicking the like button, it seems to be increasing but not in sync with the actual button count. How can I fix this issue? index.html <div class="row"> <div class="col-md-4"> ...

Trouble with $.getJSON while trying to obtain song tempo data with getSongBPM

Still learning the ropes, so bear with me. I am trying to retrieve the BPM of a song using the getSongBPM public API. However, I'm not seeing any data in the console. // Please note that the API key has been updated $.getJSON("https://api.getsongbp ...

Displaying a PHP variable on the console through the power of jQuery and AJAX

I'm attempting to display the value of a PHP variable in the browser's console using jQuery and AJAX. I believe that the $.ajax function is the key to achieving this. However, I am unsure about what to assign to the data parameter and what should ...

What is the best way to constrain an input to specific values without relying on checking keycodes or experiencing any flickering of the value

Using vue, I am looking for a way to restrict user input in an input field based on a regex pattern. I am unsure of how to achieve this. Can someone please assist me? Here is what I have tried so far: codepen.io/assassin129/pen/yGZQWe ...

What is the best way to retrieve every single element stored in an Object?

On a particular page, users can view the detailed information of their loans. I have implemented a decorator that retrieves values using the get() method. Specifically, there is a section for partial repayments which displays individual payment items, as d ...

Controller using the 'as' syntax fails to add new object to array

Lately, I've been experimenting with the Controller as syntax in Angular. However, I seem to be struggling to grasp its functionality. I am currently following a tutorial that utilizes $scope to bind the members of the controller function rather than ...

Add the file to the array field

I struggle with manipulating sub documents in MongoDB, especially when trying to implement an address book structure like the one below: { [<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e5e3f5e2a1d0fdf1f9fcbef3fffd"> ...

The SyntaxError is triggered by the React-Native FlatList component

As a newcomer to React Native, I am facing an issue with refreshing the component to load city names stored in async storage. The error occurs when I utilize the FlatList component for the first time. The specific error message I encountered is: SyntaxE ...

Javascript: object continuously moving despite key being released

When I leave the keyword new on line: var myGamePiece = new makeComponent(myContext, 30, 30, "red", 10, 120); The red square continues to move even when I release the arrow key. However, if I remove the new element from that line, the square stops moving ...

Set element back to its default state

When working with JavaScript, how do you go about restoring the default behavior of a DOM element's event handler? For instance, let's say you've set the onkeypress event for an input element: elem.onkeypress = function() { alert("Key pres ...

The function createReadStream completes execution without resolving

Currently, I am in the process of developing a program that is aimed at iterating through files within a specified directory in a Linux environment. I have implemented a custom function called "myReadFile" which returns a new promise, and it is expected ...

Incorrect font displayed on Bootstrap button after inserting hyperlink

This section contains my HTML code snippet: <div class="panel panel-default"> <div class="panel-heading"> Records <button type="button" class="btn btn-xs btn-success pull-right" id="command-add" data-row-id="1"> ...

Dealing with hefty JSON documents using iron-ajax, iron-list, and loading with iron-scroll-threshold

I'm facing an issue while attempting to load a large JSON file into my view that contains over 1000 items. My goal is to have the view import only 20 items at a time. However, every time I utilize iron-list, it only renders 3 items initially until I r ...

Using string replacement for effective search finding: Unleashing the power of substring matching

I have a method that adds an anchor tag for each instance of @something. The anchor tag links to a specific sub URL. Check out the code: private createAnchors(text: string) { return text.replace(/(@[^ @]+)/ig, '<a href="/home/user/$1">$1& ...

Show the image using material-ui-dropzone

Currently, I am engaged in a ReactJS project where I have implemented material-ui-dropzone to upload and exhibit an image. The uploading part has been successfully completed. However, my current obstacle lies in figuring out how to display this uploaded im ...

It appears that the Next.js environment variables are not defined

Upon setting up a fresh next.js project using npx create-next-app@latest and configuring some environment variables in the .env.local file, I encountered an error when attempting to run the server. "Failed to load env from .env.local TypeError: Cannot ...