Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
NewToQlikSens
Partner - Contributor II
Partner - Contributor II

How to Create bar chart using .net SDK

Hi, can anyone please help me with a sample c# code for creating bar chart on qliksense. 

I am able to create an app, sheet, dimensions and measures on qlik sense using c#, but not sure how to create barchart. I have seen documentation, but unable to find the correct samples.

Labels (4)
1 Solution

Accepted Solutions
Øystein_Kolsrud
Employee
Employee

In theory it's all about creating a generic object with the right set of properties. The right set of properties can be rather hard to figure out though, but the easiest way is to look at the properties of an existing bar chart and use that as a template. There is also this page that can help you figure out which properties are available: https://qlik.dev/apis/javascript/sn-bar-chart/

If you want to place the chart in a sheet, then you'll need to place it in the sheet as well. Same thing there: there is no absolute documentation of how to do this, so reverse engineering the properties used by the client is the best tip I can offer. This is all part of the client functionality and not really something that is handled by the engine.

If you want to look at properties of existing objects, then I can recommend this tool:

https://community.qlik.com/t5/Qlik-Sense-Documents/Qlik-Explorer-for-Developers/ta-p/1949809

You can also activate the developer menu that allows you to inspect the properties directly in the client:

https://community.qlik.com/t5/Design/Developer-menu-in-Qlik-Sense-client/ba-p/1471915

View solution in original post

6 Replies
Øystein_Kolsrud
Employee
Employee

In theory it's all about creating a generic object with the right set of properties. The right set of properties can be rather hard to figure out though, but the easiest way is to look at the properties of an existing bar chart and use that as a template. There is also this page that can help you figure out which properties are available: https://qlik.dev/apis/javascript/sn-bar-chart/

If you want to place the chart in a sheet, then you'll need to place it in the sheet as well. Same thing there: there is no absolute documentation of how to do this, so reverse engineering the properties used by the client is the best tip I can offer. This is all part of the client functionality and not really something that is handled by the engine.

If you want to look at properties of existing objects, then I can recommend this tool:

https://community.qlik.com/t5/Qlik-Sense-Documents/Qlik-Explorer-for-Developers/ta-p/1949809

You can also activate the developer menu that allows you to inspect the properties directly in the client:

https://community.qlik.com/t5/Design/Developer-menu-in-Qlik-Sense-client/ba-p/1471915

ar5
Partner - Contributor III
Partner - Contributor III

Hi,

I was trying to create a waterfallchart using dot net SDK and saw WaterfallchartProperties doesn't have a definition for "DataPoint" and "DimensionAxis", for which I was not able to see the chart in client side hub after creating sheet. This is a bug for sure. These properties are present in other types like linecharts, barcharts but not waterfall!

https://help.qlik.com/en-US/sense-developer/November2023/Subsystems/NetSDKAPIref/Content/Qlik.Sense....

private static ISheet CreateSheets(IApp application, List<SheetCell> listOfCells)
{
return application.CreateSheet("Sheet-abc",
new SheetProperties { StateName = "abc", Rank = 0, Cells = listOfCells, MetaDef = new MetaAttributesDef { Title = "Abc", Description = "" } });

}

private static IGenericObject CreateWaterfallChart(ISheet sheet, NxInfo infoVisual)
{
return sheet.CreateChild<WaterfallChart>(new GenericObjectProperties() { Info = infoVisual });
}

After I set it explicitly, I was able to see the chart in my app. Now I am not sure how to set the values. Should I deserialize json and create c# classes?
child.Properties.Set("dimensionAxis", "");
child.Properties.Set("dataPoint", "");

snippet from Json:

  "dimensionAxis": {
        "show": "labels",
        "label": "tilted",
        "dock": "near"
    },
 "dataPoint": {
        "showLabels": true
    },
Øystein_Kolsrud
Employee
Employee

No you don't really need to create C# classes. The .NET SDK includes a class called "AbstractStructure" whose main purpose is to provide a typed interface for the inherently untyped JSON structures that are used to define object properties. The following two links can tell you more:

https://help.qlik.com/en-US/sense-developer/February2024/Subsystems/NetSDKAPI/Content/Sense_NetSDKAP...
https://help.qlik.com/en-US/sense-developer/February2024/Subsystems/NetSDKAPI/Content/Sense_NetSDKAP...

There are generic "Set" and "Get" methods in that class for setting properties, and all the client object types that are available in the SDK simply extends this class and provides properties for translating C# structures to JSON and back. You don't have to go through such a class though, you can use the "Set" and "Get" methods directly. In your case you could do something like this to set the two "dimentionsAxis" and "dataPoint" properties in your "GenericObjectProperties" instance:

var o = app.GetGenericObject("<objId>");
var props = o.GetProperties();
var dimensionAxis = new DimensionAxis { Show = DimensionAxisShow.Labels, Dock = DimensionAxisDock.Near, Label = DimensionAxisLabel.Tilted };
var dataPoint = new BarchartDataPoint { ShowLabels = true };
props.Set("dimensionAxis", dimensionAxis);
props.Set("dataPoint", dataPoint);
o.SetProperties(props);

 

ar5
Partner - Contributor III
Partner - Contributor III

Thanks a bunch, now I know how to steal properties from other visual types and set them using abstract structure (if not present in the object I am trying to create). But what if I wanted to pass the full property tree of an object (propertyName:qProperty)? I do have the json of the object stored in a file. I tried using object.SetFullPropertyTree method which takes GenericObjectEntry "Property" as a parameter. It is a GenericObjectProperty which doesn't let me pass deserialized json. Tried below, no success:

var chart = app.GetGenericObject(objectId);
var props = chart.GetProperties();

var json="json";//for example

props.Set("qProperty", JsonConvert.DeserializeObject(json));

chart.SetProperties(props); //not adding hypercubes, json has qInfo,qhypercubedef

OR

chart.SetFullPropertyTree(new GenericObjectEntry() { Property = JsonConvert.DeserializeObject(json) });//error

Øystein_Kolsrud
Employee
Employee

The method "SetFullPropertyTree" sets the properties of the generic object and all its children. It looks to me like you just want to do "SetProperties" here, and in that case you could do something like this:

var chart = app.GetGenericObject(objectId);
var json = "json";//for example
var jObj = JObject.Parse(json);
var absStruct = new AbstractStructure(jObj);
var props = absStruct.As<GenericObjectProperties>();
chart.SetProperties(props);

I've written all the steps on separate lines here to be as clear as possible. The steps are:

  1. Create a JObject instance from the JSON string.
  2. Create an AbstractStructure instance from the JObject.
  3. Interpret the AbstractStructure instance as a "GenericObjectProperties".

You could of course inline it all like this:

var props = new AbstractStructure(JObject.Parse(json)).As<GenericObjectProperties>();

 

ar5
Partner - Contributor III
Partner - Contributor III

Thank you so much for the help. It did work as expected.