Skip to content

A chart filter proposal

This is a proposal of a filter function in the chart view. Add following code in the attributes, and reload the chart.

Filter
const initialDate = context.getNow().toString()
let referenceFromDate = initialDate.toString()
let referenceToDate = initialDate.toString()

const filter = [
{
    type: "Date",
    name: "StartDate",
    title:"Period", 
    userDescription:"", 
    resolution: 'day',
    value: initialDate,
    onChanged:function(currentConfig){
      setDate(currentConfig)
   }
},
  {
    type: "Button",
    name: "previous",
    text: t("core:previous"),
    userDescription: "previous period",
    enabled: true,
    visible: true,
    onClicked: function (currentConfig) {
      previous(currentConfig)
   }
  },
  {
    type: "Radio",
    name: "timeSpan",
    userDescription: "The period",
    enabled: true,
    visible: true,
    value: "1 w",
    onChanged: function (currentConfig) {
      setDate(currentConfig);
    },
    data: [
      {
        title: t("core:month"),
        value: "1 m",
        visible: true,
        enabled: true,
      },
      {
        title: t("core:week"),
        value: "1 w",
        visible: true,
        enabled: true,
      },
      {
        title: t("core:day"),
        value: "1 d",
        visible: true,
        enabled: true,
      },
      {
        title: "12 h",
        value: "12 h",
        visible: true,
        enabled: true,
      },
      {
        title: "6 h",
        value: "6 h",
        visible: true,
        enabled: true,
      },
      {
        title: "1 h",
        value: "1 h",
        visible: true,
        enabled: true,
      },
    ],
  },
  {
    type: "Button",
    name: "next",
    text: t("core:next"),
    userDescription: "next period",
    enabled: true,
    visible: true,
    onClicked: function (currentConfig) {
      next(currentConfig)
    }
  },
  {
    type: "Button",
    name: "now",
    text: t("core:now"),
    userDescription: "now",
    enabled: true,
    visible: true,
    onClicked: function (currentConfig) {
      now(currentConfig)
    }
  },
  {
    type: "Button",
    name: "forceDump",
    text: "Hämta Loggar",
    userDescription: "now",
    enabled: true,
    visible: true,
    onClicked: async function (currentConfig) {
      await context.call(".synchronize");
    }
  }
];

function next(currentConfig){
  const currentDate = currentConfig[0].value.toString()
  const newFromDate = context.setFromDate(currentDate,currentConfig[2].value)
  currentConfig[0].value = newFromDate
  context.setToDate(newFromDate, currentConfig[2].value)
}

function previous(currentConfig){
  const currentDate = currentConfig[0].value.toString()
  const newFromDate = context.setFromDate(currentDate,"-"+currentConfig[2].value)
  currentConfig[0].value = newFromDate
  context.setToDate(newFromDate, currentConfig[2].value)
}
function now(currentConfig){
  const now = context.getNow().toString()
  const newFromDate = context.setFromDate(now, "-15 min")
  currentConfig[0].value = newFromDate
  context.setToDate(newFromDate, "17 min")
}

function setDate(currentConfig){
  let date = currentConfig[0].value
  context.setFromDate(date)
  context.setToDate(date,currentConfig[2].value)
}

function setInitialDate(currentConfig){
  let date = currentConfig[0].value
  let initialFromDate = context.setFromDate(date, "-" + currentConfig[2].value).toString()
  currentConfig[0].value = initialFromDate
  let initialToDate = context.moment(date).add("12", "hours").toString()
  context.setToDate(initialToDate)
}

setInitialDate(filter)
return filter
ServerSideJS
return { 
        synchronize : async function synchronize(args, {context}){
            const result = await context.legacy.execute("%Controller%.Synchronize");
            console.log(result)
            return 0;
        }
    }

This proposal is not yet fully supported for language translations, but works for most target views.