Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Iceroom
New Member

Set y-axis 'max height' and 'min height' with R based bar graph visual

Hi guys. I'm trying to create a R based bar chart power Bi visual for practicing custom visual creation. I want to create an input field in the Format of Power Bi that defines the max and min heights of the Y axis. I saw a lot of examples but I didn't get enough inspiration.

I have successfully created an input area in capabilities.json. I have set related parameters in setting.ts. I also set the x and y axes in the script.r file and made them a bar graph, but I can't achieve the maximum and minimum height of the y axis with my code. I tried to use ylim but it does not work. Can someone help?

"yAxis": {
      "displayName": "Y-Axis",
      "displayNameKey": "Visual_YAxis",
      "properties": {
        "show": {
          "displayName": "Show",
          "displayNameKey": "Visual_Show",
          "type": {
            "bool": true
          }
        },
        
        "dynamicScaling": {
          "displayName": "Scaled",
          "displayNameKey": "Dynamic_Scaling",
          "type": {
            "bool": true
          }
        },
        "yscalemaxin":{
          "displayName": "Maximum Height",
          "displayNameKey": "dynymax",
          "type": {
            "numeric": true
          }
        },
        
        "yscaleminin":{
          "displayName": "Minimum Height",
          "displayNameKey": "dynymin",
          "type": {
            "numeric": true
          }
        }
        
      }
    }

 

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly");
libraryRequireInstall("htmlwidgets");
####################################################

################### Actual code ####################
dataFrame <- data.frame(Axis, Values[[1]]);
dynamicScaling = TRUE;

if(exists("yAxis_dynamicScaling")){
	dynamicScaling = as.logical(yAxis_dynamicScaling);
}


if(dynamicScaling){
yscaleminin = 0
yscalemaxin = Inf	
if(exists("yAxis_yscaleminin")){
		yscaleminin = as.numeric(yAxis_yscaleminin)
		
		yscalemininValue = yAxis_yscaleminin

		yscalemininValue = round(max(min(yscalemininValue,1e+6),0))
	}else{
		yscalemininValue = 0; #if user does not enter a value then the y-axis start at 0
	}

if(exists("yAxis_yscalemaxin")){
		yscalemaxin = as.numeric(yAxis_yscalemaxin)
		
		yscalemaxinValue = yAxis_yscalemaxin

		yscalemaxinValue = round(max(min(yscalemaxinValue,10000),0))
	}else{
		yscalemaxinValue = Inf; #if user does not enter a value into y-axis end, the y-axis end at infinity
	}


			yscales <- c(-Inf, yscalemaxinValue, yscalemaxinValue, Inf)
			scale <- c(yscalemaxinValue,yscalemaxinValue)[findInterval(dataFrame[[2]], vec=yscales)]
			
			p <- plot_ly(Values,
    
				x = dataFrame[[1]],
				y = dataFrame[[2]],
				type = "bar",
				marker = list(yscales = scale)

				
			);
			}else{ #No dynamic scaling
		p <- plot_ly(Values,
		x = dataFrame[[1]],
		y = dataFrame[[2]],
		type = "bar",
		
	);

			
}

####################################################

############# Create and save widget ###############
#p = ggplotly(p);
internalSaveWidget(p, 'out.html');
####################################################

 

2 REPLIES 2
Iceroom
New Member

Hi guys. I'm trying to create a R based bar chart power Bi visual for practicing custom visual creation. I want to create an input field in the Format of Power Bi that defines the max and min heights of the Y axis. I saw a lot of examples but I didn't get enough inspiration.

I have successfully created an input area in capabilities.json. I have set related parameters in setting.ts. I also set the x and y axes in the script.r file and made them a bar graph, but I can't achieve the maximum and minimum height of the y axis with my code. Can someone help?

"yAxis": {
      "displayName": "Y-Axis",
      "displayNameKey": "Visual_YAxis",
      "properties": {
        "show": {
          "displayName": "Show",
          "displayNameKey": "Visual_Show",
          "type": {
            "bool": true
          }
        },
        
        "dynamicScaling": {
          "displayName": "Scaled",
          "displayNameKey": "Dynamic_Scaling",
          "type": {
            "bool": true
          }
        },
        "yscalemaxin":{
          "displayName": "Maximum Height",
          "displayNameKey": "dynymax",
          "type": {
            "numeric": true
          }
        },
        
        "yscaleminin":{
          "displayName": "Minimum Height",
          "displayNameKey": "dynymin",
          "type": {
            "numeric": true
          }
        }
        
      }
    }

 

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly");
libraryRequireInstall("htmlwidgets");
####################################################

################### Actual code ####################
dataFrame <- data.frame(Axis, Values[[1]]);
dynamicScaling = TRUE;

if(exists("yAxis_dynamicScaling")){
	dynamicScaling = as.logical(yAxis_dynamicScaling);
}


if(dynamicScaling){
yscaleminin = 0
yscalemaxin = Inf	
if(exists("yAxis_yscaleminin")){
		yscaleminin = as.numeric(yAxis_yscaleminin)
		
		yscalemininValue = yAxis_yscaleminin

		yscalemininValue = round(max(min(yscalemininValue,1e+6),0))
	}else{
		yscalemininValue = 0; #if user does not enter a value then the y-axis start at 0
	}

if(exists("yAxis_yscalemaxin")){
		yscalemaxin = as.numeric(yAxis_yscalemaxin)
		
		yscalemaxinValue = yAxis_yscalemaxin

		yscalemaxinValue = round(max(min(yscalemaxinValue,10000),0))
	}else{
		yscalemaxinValue = Inf; #if user does not enter a value into y-axis end, the y-axis end at infinity
	}


			yscales <- c(-Inf, yscalemaxinValue, yscalemaxinValue, Inf)
			scale <- c(yscalemaxinValue,yscalemaxinValue)[findInterval(dataFrame[[2]], vec=yscales)]
			
			p <- plot_ly(Values,
    
				x = dataFrame[[1]],
				y = dataFrame[[2]],
				type = "bar",
				marker = list(yscales = scale)

				
			);
			}else{ #No dynamic scaling
		p <- plot_ly(Values,
		x = dataFrame[[1]],
		y = dataFrame[[2]],
		type = "bar",
		
	);

			
}

####################################################

############# Create and save widget ###############
#p = ggplotly(p);
internalSaveWidget(p, 'out.html');
####################################################

 

v-viig
Community Champion
Community Champion

We'd recommend to use lower case for all of settings. In other words, please rename yAxis to yaxis.

 

Please let us know if such workaround works well on your side.

 

Ignat Vilesov,

Software Engineer

 

Microsoft Power BI Custom Visuals

pbicvsupport@microsoft.com

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors