project-nv / night-vision

Highly customizable charting library, created for professional traders
https://nightvision.dev
MIT License
237 stars 54 forks source link

Log scale won't work #76

Closed mariusvigariu closed 10 months ago

mariusvigariu commented 1 year ago

Describe the bug

Adding log = true to scale settings will result in an error:

chart.data = {
  panes: [
    {
      overlays: [
        {
          // ...
        },
      ],
      settings: {
        scales: {
          A: {
            log: true,
          },
        },
      },
    },
  ],
};

this patch would apparently fix the issue:

diff --git a/src/core/gridScale.js b/src/core/gridScale.js
index e0e1aa9..be17b78 100644
--- a/src/core/gridScale.js
+++ b/src/core/gridScale.js
@@ -255,6 +255,10 @@ export default function Scale(id, src, specs) {
         self.$_mult = dollarMult()
         self.ys = []

+        const ov = ovs[0]
+        console.log(ov)
+        const data = ov.dataSubset
+
         if (!data.length) return

         let v = Math.abs(data[data.length - 1][1] || 1)
@@ -316,6 +320,8 @@ export default function Scale(id, src, specs) {
         return y$
     }

+    let MAX_INT = Number.MAX_SAFE_INTEGER
+
     // Make log scale levels look great again
     function logRounder(x, quality) {
         let s = Math.sign(x)

but the values would still not be properly rendered on the sidebar

image

and in the example below the first candle has a huge upper wick which gets rendered till infinite (see legend vs sidebar)

image

Reproduction

https://nightvision.dev/guide/intro/getting-started.html

Steps to reproduce

Follow the getting started steps then set settings in main.js

      settings: {
        scales: {
          A: {
            log: true,
          },
        },

Javascript Framework

vue

Logs

No response

Validations

Antc1993 commented 11 months ago

Hi, did you find a solution? I have the same problem.

mariusvigariu commented 11 months ago

Looks like recent commits fix some parts of the issue. I still see room for improvement for cases where the sidebar needs to render small values, eg:

image

I've dig a little bit in the code and I think Scale.ys needs to be tweaked a little bit, but dont have the time to understand the code properly.

@C451 do you have some pointers on this? I could make a PR with the right guidance

C451 commented 10 months ago

I fixed log scale a bit, but the y-transforms work incorrectly. TODO for the future. Log scale is tricky, even TV goes it somewhat funky